Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extends vs implements

 
Andrew Shafer
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anyone wanna take a crack at explaining the theoretical difference between extending an abstract class or implementing an interface?
 
Qudsia Jaffery
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic