• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how Interface supports polymorphism?

 
kiran nyala
Ranch Hand
Posts: 55
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use text colouring only for highlighting.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15644
48
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic