Using superclass to initialise a subclass object java [duplicate]

You may have a method that only takes an instance of SuperClass. Since SubClass is a SuperClass, you can use an instance of SubClass and treat it as SuperClass.

The same behavior is used when working with interfaces:

List someList = new ArrayList();

That’s the beauty of polymorphism. It allows you to change out the implementation of the class’ internals without breaking the rest of your code.

Leave a Comment