Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Classes Vs Interfaces

 
Lavanya Mothilal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a doubt regarding interfaces. An abstract class with all the methods declared abstract is same as an interface. i.e; the concrete class which extends the abstract class or the one implementing the interface should define all the methods. This is fine. So Is the only advantage of interfaces is Multiple inheritance ? Or is there any other ? Can anyone explain.
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. there is one advantage.
do you have Multiple inheritance of interface but not can have Multiple inheritance of class.
example:




but this correct look:


class and interface are differents.
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's another advantage that is to define a contract between the classes.
I guess that it's one of the benefits of using OOP: reusable code. Beside, the maintenance of the code is easier to do.

Am I wrong?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces allow you to define common behavior among otherwise very different objects; like sailboats and ducks.

A sailboat object could be a subclass of Boat, and a duck could be a subclass of Bird. Even though they are completely different objects, they have some common traits.



They can both float and they both have the means to move in the water; one with sails and the other with webbed feet.
So even though they are radically different objects, a method that operates on a Floatable, can expect that it will float and has a propel method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic