Skip to content
Advertisement

Store in Array List two child classes of the same parent class

I have a parent class User with only Attributes Username and Password. Also i have two Child Classes Customer and Admin. Can i store to the same ArrayList both customers and admins? I need to use the username and password attributes to authenticate them for the log in.

Advertisement

Answer

yes , you can make an Arraylist of User class and when you call the constructor you can use any one of the three classes

see this tutorial http://www.tutorialspoint.com/java/java_polymorphism.htm

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement