Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict elements of an ArrayList without using generics

Suppose I have an Employee class. How can I implement an ArrayList only containing Employee elements without using generics? That is, without Arraylist<Employee>, how can I restrict the ArrayList to add only Employee objects?

like image 626
sartysam Avatar asked Dec 06 '25 08:12

sartysam


1 Answers

Extend ArrayList and customize add() and addAll() method to check the object being added is instanceof Employee

like image 170
jmj Avatar answered Dec 08 '25 21:12

jmj