• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

extends vs implements

 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Intermediate Java seemed like as good a place as any for this question.
I'm looking for some clarity concerning the use of interfaces vs subclassing. If anyone (especially bartender anyones) could recommend a site or a thread where this is discussed, I'd be much obliged.
Are interfaces just away to get around java disallowing multiple inheritance for subclasses?
Thanks
Andrew
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Over in the OO forum. Go ask them why the GoF (Gang of Four) says "always program to an interface, not an implementation". You will get all sorts of fun responses .
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface defines a set of methods but does not implement them. A class that implements the interface has to implement all the methods defined in the interface.
Basically--
An interface cannot implement any methods, whereas a class implements them.
A class can implement many interfaces but can have only one superclass.
An interface is not part of the class hierarchy. Unrelated classes can implement the same interface.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is some good reading: http://www.javaranch.com/ubb/Forum1/HTML/000678.html
and http://www.javaranch.com/ubb/Forum33/HTML/002016.html
Theres lots more if you search.
 
Andrew Shafer
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Cindy and Parmeet,
I will pose the question in OO and see who will bite over there.
Parmeet, I understand how an interface works in practice, but in theory I'm a bit confused about the difference between extending an abstract class or implementing an interface.
It just seems like a way to get multiple inheritance.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andrew Shafer:

Parmeet, I understand how an interface works in practice, but in theory I'm a bit confused about the difference between extending an abstract class or implementing an interface.
It just seems like a way to get multiple inheritance.


Keep in mind the Java language comes from Sun, where I'm sure many developers are ex-C++ (multiple inheritance) and ex-Smalltalk (single inheritance) developers. Smalltalk won.
Seriously, from an OO standpoint, I personally love multiple inheritance, but it does present management/development problems as projects and teams grow. All it takes is one circular reference to make compilation a tedious task.
Implementing interfaces in Java does (to an extent) work around the problems of "pure" multiple inheritance, but also adds the ability for any class to implement any interface (no need for the classes to be part of the same inheritance tree).
The tradeoff is when (if) you change the interface by adding a method, any classes implementing the method will have to change to reflect the additional method. Using abstract classes can avoid this, since you can add a non-abstract method to an abstract class without harming classes that extend the abstract.
Having developed for years with C++, I used to complain - loudly - about the lack of multiple inheritance in Java. But I'm gradually coming around to the idea of the Java extend/implement scheme. Now if I could only come to grips with the language's lack of pointers...
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is a common problem you face when you're new to class design.
This should be seen from OO perspective.
Interface implementation versus composition.

The "correct" answer depends upon the relationships between your classes/objects. An interface implementation or subclassing is indicated by a "is-a" relationship whereas a "has-a" relationship indicates composition.
eg. A Sedan "is-a" LandVehicle
A Sedan "has-a" Engine
If your class falls under is-a category - go for abstraction else
define interface (with all common has-a characteristics as methods ) and implement it in your class.
There's one exception to this:
Since JAVA doesn't support multiple inheritence - when you need subclass from more than one class - use interface
There's a book that may help you with the design issues. I don't remember the title exactly. The author is Robert Martin. He's the founder of Object Mentor corporation. I believe it's www.objectmentor.com.
This is my 2 cents worth!.
maalti


Interfaces allow you to be a "subclass" of lots of classes at
once without having to use multiple inheritence proper.

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope Josh doesn't mind me pre-empting him here and quoting from his book. In "Effective Java", he writes: Item 16: Prefer interfaces to abstract classes. The reasons he gives include:

  • Existing classes can be easily retrofitted to implement a new interface
  • Interfaces are ideal for mixins
  • Interfaces allow the construction of nonhierarchical type frameworks

  • There are other reasons and details he gives but you'll just have to either buy the book, win it here, or get Josh to explain them
 
Author and "Sun God"
Posts: 185
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by JUNILU LACAR:
I hope Josh doesn't mind me pre-empting him here and quoting from his book


On the contrary, thanks! I can't monitor all of the topics all of the time, it's nice to get some help I'll just add that I discussed a closely related topic yesterday.

------------------
Joshua Bloch
Author of Effective Java
 
reply
    Bookmark Topic Watch Topic
  • New Topic