aspose file tools
The moose likes Java in General and the fly likes Question to Authors:Interfaces VS Abstract Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Question to Authors:Interfaces VS Abstract Classes" Watch "Question to Authors:Interfaces VS Abstract Classes" New topic
Author

Question to Authors:Interfaces VS Abstract Classes

M Jay
Ranch Hand

Joined: Sep 21, 2004
Posts: 66
Hi,

What should be considered when making a decision on using Java Interfaces vs Abstract Classes? or which should be used where?

Thanks


SCJP J2SE 1.4<br />SCBCD J2EE 1.3
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

I realize I'm not the "Authors" you're referring to, but here are some helpful guidelines:

Abstract classes vs. interfaces: When does it make sense to choose an abstract class over an interface?
Abstract classes and interfaces practicum: Move from theory to practice on when to employ abstract classes vs. interfaces
Tech Tips: ABSTRACT CLASSES VS. INTERFACES
Why extends is evil
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Why is this a question to the authors???


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joshua Bloch
Author and "Sun God"
Ranch Hand

Joined: May 30, 2001
Posts: 124
I do discuss this one in a fair amount of detail in Effective Java Item 16 ("Prefer interfaces to abstract classes"). Here's the summary:

An interface is generally the best way to define a type that permits multiple implementations. An exception to this rule is the case where ease of evolution is deemed more important than flexibility and power. Under these circumstances, you should use an abstract class to define the type, but only if you understand and can accept the limitations. If you export a nontrivial interface, you should strongly consider providing a skeletal implementation to go with it. Finally, you should design all of your public interfaces with the utmost care and test them thoroughly by writing multiple implementations.

Regards,

Josh


Joshua Bloch <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0201310058/ref=ase_electricporkchop" target="_blank" rel="nofollow">Effective Java</a> and coauthor of <a href="http://www.amazon.com/exec/obidos/ASIN/032133678X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Java Puzzlers</a>
M Jay
Ranch Hand

Joined: Sep 21, 2004
Posts: 66
Steve,

Thanks for the links, very helpful. Ilja, as those guys have written a book about Java, I guessed they would have the exact correct answer for me, maybe there is something in their book that they have written regarding this, especially as their book seems to be gearded towards experienced coders.
Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
I am not an author, but I'll try.

If you want to generalize the behavior of a bunch of classes, then you create an interface and make the classes implement that interface. A good example is the Java Collections Framework, where for example, the List interface defines the behavior of any implementation of a List data structure (like ArrayList, Vector, etc.)

If you want to generalize the structure (and may be a little bit of behavior) of a bunch of classes, then you create an abstract class. A good example is the JComponent class in Swing, which is the common class to most of the widgets in Swing.


Cheers, Sathya Srinivasan - SCJP 1.2, SCWCD 1.2, SCMAD 1.0
Co-Author of Whizlabs SCMAD Certification Exam Simulator and SCMAD Exam Guide Book
Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
Originally posted by M Jay:
Steve,

Thanks for the links, very helpful. Ilja, as those guys have written a book about Java, I guessed they would have the exact correct answer for me, maybe there is something in their book that they have written regarding this, especially as their book seems to be gearded towards experienced coders.


Interfaces and Abstract classes are really Object-Oriented Programming concepts and not really specific to Java. By directing a question to a specific set of people (not that Joshua can't answer it - he has written some of the best books in Java), you are losing the perspectives of different people from different backgrounds and areas of expertise, which normally comes in handy.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Sathya Srinivasan:

Interfaces and Abstract classes are really Object-Oriented Programming concepts and not really specific to Java.


In my view, interfaces are specific to languages without multiple inheritance. In languages with multiple inheritance, interfaces are equivalent to fully abstract classes.

Or, with other words, the only difference between an interface and a fully abstract class in java is that the interface allows for multiple inheritance.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1697
Originally posted by Sathya Srinivasan:
I am not an author, but I'll try.

If you want to generalize the behavior of a bunch of classes, then you create an interface and make the classes implement that interface. A good example is the Java Collections Framework, where for example, the List interface defines the behavior of any implementation of a List data structure (like ArrayList, Vector, etc.)

If you want to generalize the structure (and may be a little bit of behavior) of a bunch of classes, then you create an abstract class. A good example is the JComponent class in Swing, which is the common class to most of the widgets in Swing.



This is very good explanation with nice example.
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 354
hello,
Originally posted by Sathya Srinivasan:

If you want to generalize the behavior of a bunch of classes, then you create an interface and make the classes implement that interface. A good example is the Java Collections Framework, where for example, the List interface defines the behavior of any implementation of a List data structure (like ArrayList, Vector, etc.)

If you want to generalize the structure (and may be a little bit of behavior) of a bunch of classes, then you create an abstract class. A good example is the JComponent class in Swing, which is the common class to most of the widgets in Swing.

I disagree with that, and the swing API is not designed very well. If there are common features that many implementing classes of an interface would need, then make an abstract class that implements your interface. An example for this design is java.util.List and java.util.AbstractList. With this approach clients of your API can still make their own interfaces which extend your interface. If you would only provide an abstract class that would not be possible. This is only one of many reasons why the strict "abstract class" approach is wrong.

Using an abstract class as a type (declaration, parameter or return type) violates the principle of programming to an interface.

I wrote something about that here. We also had the same discussion a couple of weeks ago in this thread.

Kai


Kai Witte's business website Kai Witte's private homepage Mock exam / preparation kit reviews
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Question to Authors:Interfaces VS Abstract Classes
 
Similar Threads
Interfaces Vs Abstract Classes
abstract classes vs interfaces
Abstract classes vs. interfaces
Abstract Class vs. Interface
abstract Vs interface : usability