Polymorphisim in Java stands for many forms what can be achieved by overriding sub-class method. Regarding generics which allows to pass in generic values, such as ArrayList<Object>
. Is that part of polymorphism concept?
Cheers
Generics and Polymorphism are two different things.
Generics are used primarily to specify which type is expected. You can use wildcards to define a range of types. E.g. <? extends List>
could apply to any type of List (LinkedList
, ArrayList
)
Polymorphism is the concept that an object can have many types. So an object can be an instance of List and an instance of ArrayList
as an example.
For example, imagine three classes
public class Animal
public class Dog extends Animal
public class Cat extends Animal
If you have an instance of Dog
, that object is both a Dog
AND an Animal
.
Obviously the two fit together quite nicely as if you define an ArrayList<Object>
then you can add any Object
to that list (Which is any class in Java)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With