File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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?

Qudsia Jaffery

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 Jaffery

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
There was a good
article in April 2001 issue of Javaworld. You might find it interesting and informative too.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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,
[This message has been edited by Desai Sandeep (edited June 28, 2001).]
I agree. Here's the link:
subject: extends vs implements
It's not a secret anymore!