Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use abstract class instead of interfaces

 
Ugochukwu Onwordi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic