Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arraylist and Inheritance in Java [closed]

How do I put two different objects from two different classes which inherit from the same super class in one single array list?

like image 518
Gigom Avatar asked May 07 '26 11:05

Gigom


1 Answers

List<Parent> listValue = new ArrayList<Parent>();
listValue.add(new Child1());
listValue.add(new Child2());

Both Child1 and Child2 are inherited from "Parent" Class.

like image 141
Thilak Avatar answered May 10 '26 00:05

Thilak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!