File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes extends vs implements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "extends vs implements" Watch "extends vs implements" New topic
Author

extends vs implements

Andrew Shafer
Ranch Hand

Joined: Jan 19, 2001
Posts: 338

Anyone wanna take a crack at explaining the theoretical difference between extending an abstract class or implementing an interface?


!_I_Know_Kung_Fu_!
Qudsia Jaffery
Greenhorn

Joined: Jun 20, 2001
Posts: 16
There is good discussion on the subject in the first Chapter of 'Design Patterns' by Gama, helm, Jhonson, Vlissides. I qoute from the book

Qudsia
Qudsia Jaffery
Greenhorn

Joined: Jun 20, 2001
Posts: 16
I am sorry about breaking this into two messages, however here is the qoute
"An objects class defines how the obect is implemented. The class defines the objects internal state and the implementation of its operations. In contrast an objects type only refers to its interface - the set of requests to which it can respond. An object can have many types and objects of different types can have the same type."
Some differences that I can think of are
An abstrct class may have some concrete method and some attributes
All methods of an interface need to be impemented, in the class that extends the interface
Ramesh Donnipadu
Ranch Hand

Joined: Sep 16, 2000
Posts: 100
Andrew,
There was a good
article in April 2001 issue of Javaworld. You might find it interesting and informative too.
-Ramesh
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
There is one definition of abstract class that I can think of - If you are certain that the instance of class can ONLY be one of the subtypes instances then define that class as abstract.
Larman (Applying UML and Patterns) illustrates this with an example of Payment as an abstract class, since its instance can only be of Cash,Credit and Check types.
Another use of abstract class is in Template pattern - An abstract class contains a concrete method and another non-concrete/abstract methods.These abstract methods are implemented in the subtypes.In the abstract class, the non-concrete methods are called in the concrete method, i.e the basic template of what needs to be done is defined in the abstract class.
As regards interface, one definition which I can think of is instance of a class can't be predicted.The class could then be defined as an interface, where the concrete subtypes which implement this interface could act as different plug-ins.
Would also like to add, that when you extend a class, you are inheriting both the capability and responsibility of the superclass.However, when you implement the interface, you are only inheriting the responsibility of the supertype.
Hope this helps,
Sandeep
[This message has been edited by Desai Sandeep (edited June 28, 2001).]


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: extends vs implements