4

Possible Duplicate:
When to use an interface instead of an abstract class and vice versa?

Can any one tell me under which circumstances we should go for interface and abstract class.

Java specific aspects are welcome.

Community
  • 1
  • 1
Hari
  • 245
  • 3
  • 6
  • 13

6 Answers6

3

Always use an interface unless you need to ...

  • ... provide subclasses with some state
  • ... provide subclasses with default implementations of some methods
  • ... want to defer implementation of some of the abstract methods

Note that you can only extend one class, while you can implement multiple interfaces. So if there's any chance that a subclass will need to extend some other class, strive for using an interface.

Here are some good links that discusses this topic:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
1

In simple Language :

Use interface if you want your objects be accessed by common way. Use abstract class if you want to define some functionality in super class and to define prototype of some methods that must be override in child classes i.e., extending the functionality of a class. Here is a funny example that might help you to clear the fundamentals.

http://ganeshtiwaridotcomdotnp.blogspot.com/2011/05/understanding-importance-of-interface.html

gtiwari333
  • 24,554
  • 15
  • 75
  • 102
0

If you don't want to implement any method and you just want to define your contract, then you use an interface.

However, if you do want to have some implementation already, you should use an abstract class.

wjans
  • 10,009
  • 5
  • 32
  • 43
0

Check these

Srikanth Venkatesh
  • 2,812
  • 1
  • 20
  • 13
0

You will use an abstract class if you want to provide a partial implementation for the subclasses to extend, and an interface if you only want to provide signatures of methods that must be implemented. It is perfectly normal to provide both and interface and an abstract class that implements parts of it.

There is however one limitation of abstract classes: In a subclass you can only extend one (abstract) class, but you may implement as many interfaces as you like in a single class.

Mathias Schwarz
  • 7,099
  • 23
  • 28
0

Interfaces are simply a collection of public method signatures and public static final fields. No constructors, no protected/internal methods, no other type of fields.

On the other hand, any class can be abstract simply by putting abstract in front of its declaration. They can declare abstract methods and implement interfaces and other abstract classes without defining the method implementation.

An abstract class is more restrictive when it comes to inheritance (only one can father a subclass), but you can implement methods and constructors in it.

Any number of interfaces can be implemented by a class, but there is no default method & constructor implementation.

That is why it is always a good idea to provide an abstract class next to an interface as a default implementation option.

Nick
  • 5,765
  • 5
  • 27
  • 36