4

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

uml
  • 1,171
  • 4
  • 16
  • 28
  • I think this link may contain interesting information for you: [Is polymorphism possible without inheritance](http://stackoverflow.com/a/11732581/697630). It covers the different types of polymorphism, including parametric polymorphism, which is the one you are talking about here. – Edwin Dalorzo Dec 20 '12 at 11:26

4 Answers4

3

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)

cowls
  • 24,013
  • 8
  • 48
  • 78
3

Polymorphism in java can be achieved by two ways in java.

  1. static (overloading - compile time )
  2. dynamic (overriding - runtime)

Regarding your question

Yes it can be done by overriding method in subclass. You can genrics objects as well in arguments in both overloading as well as overriding.

Rais Alam
  • 6,970
  • 12
  • 53
  • 84
2

Polymorhphism cannot be applied to generic types. Although String extends Object, ArrayList<Object> cannot be used to refer ArrayList<String>

Object obj = new String(); // OK
ArrayList<Object> list = new  ArrayList<String>(); // Not OK
Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • Many tend to think about polymorphism in terms of the object-oriented programming model, but the truth is the polymorphism is a broader concept and in other programming paradigms it is implemented in different ways. In functional programming languages like Haskell or SML parametric polymorphism is key. Java generics share a lot of similarities with it. – Edwin Dalorzo Dec 20 '12 at 11:35
1

Using Object for polymorphism is considered a bad pattern in Java. It does not give you the assurances and type safety that an interface would give you.

List<Object> animals = new ArrayList<Object>();
animals.add(new Sheep());
animals.add(new Cat());
// This will throw us an error! (ClassCastException)
Cat cat = (Cat) animals.get(0)

The error demonstrated above cannot be caught at compile time and will only be thrown when the error occurs at runtime. We could have completely avoided this error if we had created an Animal interface.

interface Animal {
     //other methods you need to hide through polymorphism
     void sound();
}
List<Animal> animals = new ArrayList<Animal>();
// Sheep and Cat implement the Animal interface
animals.add(new Sheep());
animals.add(new Cat());
// No error
Animal animal = animals.get(0)
// This will through a COMPILE TIME error
Cat cat = animal.get(1);

This shows a better way of trying to a achieve polymorphism, we have created a safer way when can be checked at compile time not at runtime. Try to avoid using Object wherever possible, there is probably a better Interface you can design or use for you needs.

Tom
  • 15,798
  • 4
  • 37
  • 48