wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Abstract Classes Vs Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract Classes Vs Interfaces" Watch "Abstract Classes Vs Interfaces" New topic
Author

Abstract Classes Vs Interfaces

Lavanya Mothilal
Ranch Hand

Joined: Dec 27, 2007
Posts: 30
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.


Thanks,<br />Lavanya
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
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.


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Abstract Classes Vs Interfaces