aspose file tools*
The moose likes Beginning Java and the fly likes When to use abstract class instead of interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When to use abstract class instead of interfaces" Watch "When to use abstract class instead of interfaces" New topic
Author

When to use abstract class instead of interfaces

Ugochukwu Onwordi
Greenhorn

Joined: Oct 25, 2007
Posts: 11
Hi,people.Please let me know what situations are best suited for abstract classes instead of interfaces.Thanks I appreciate any help I can get.
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
The best use for Interfaces I would say is to simulate Multiple Inheritance.
Abstract classes are usually parent classes that contain methods that Behave differently for every child class. So use an Abstract class when you want a parent class that is so general that shouldn't be instantiated, but defines what its children should do.
But if your subclass IS something more besides being an extension of its parent, that something more should be defined by an Interface.
Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
I disagree slightly from Jose, or maybe just with his verbage.

Interfaces are used to define the contracts that your class agree's to. For example, if you implement the comparable interface, you agree to implement a compare method that adhere's to the contract. Interfaces are a way to enhance the useability of your class, because a lot of API methods accept instances of an interface as an argument.

I don't know of any API method that has an abstract class as a parameter. Abstract classes implement partial functionality. I generally extend an abstract class to avoid writing the same code for several different, but similar classes.

For a more concrete example, lets think of an abstract Car class, and all car's have the same "drive" functionality.

We might say

public class Toyota extends Car() {
//implement custom Toyota stuff...
}

but now, since it extends car - It can do drive();

You will rarely refer to an object by it's abstract class, but moreso by it's interface.

I hope this helps.
Ugochukwu Onwordi
Greenhorn

Joined: Oct 25, 2007
Posts: 11
Thanks alot guys I really appreciate the help.I think i'm a bit clearer on the issue,but now i've got another question.What is multiple inheritance and how does it help?
Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
Multiple Inheritance is where a class extends two different classes - for example:

public class Person extends Human, Mammal

That's *NOT* valid syntax - what happens if both Human and Mammal have a breathe() method? Which one is called?

With Composition, Person could internally implement Human and Mammal. This is a somewhat bad example, because it should probably go Mammal -> Human -> Person - and not on a different heirarchy, but hopefully this helps.

To quote Josh Bloch "Favor composition to inheritance".

...but if you really ARE trying to extend the heirarchy, do it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Originally posted by Adam Schaible:
I don't know of any API method that has an abstract class as a parameter.

How about every paint method inside AWT / Swing that takes a Graphics object as a parameter?
Component.paint(Graphics)
Component.paintAll(Graphics)
Component.print(Graphics)
Component.printAll(Graphics)
JComponent.paintBorder(Graphics)
JComponent.paintChildren(Graphics)
JComponent.paintComponent(Graphics)

To name a few


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
I've actually never used AWT - most of my work is on the server.

I didn't say there weren't any, I just said I wasn't familiar with any. Thumb's up, sir.
 
Consider Paul's rocket mass heater.
 
subject: When to use abstract class instead of interfaces