It's not a secret anymore!
The moose likes Beginning Java and the fly likes how Interface supports polymorphism? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how Interface supports polymorphism?" Watch "how Interface supports polymorphism?" New topic

how Interface supports polymorphism?

kiran nyala
Ranch Hand

Joined: Sep 21, 2009
Posts: 50


i understand that an interface can implement by any number of classes so that we can say that interface supports polymorphism.
is my view correct?

Why we assign subclass object to super class reference in case of class inheritance and also in case of interface implementation? could you please explain

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Please use text colouring only for highlighting.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Your second question is a frequently asked question on the forums here. Look at the following example:

The good thing about the first version is that the rest of the application that uses the list does not need to know what kind of list exactly it is - the only thing that's important is that it is a list. Because of this, you could easily change the exact type of the list if you later find out that that would be more efficient, for example:

For the rest of the application, names is still a List<String>, so you would not have to change anything in the rest of the application.

It's the principle of program to an interface, not an implementation.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: how Interface supports polymorphism?
jQuery in Action, 3rd edition