4

In an interview it was asked to me to justify when to choose interface and when to choose abstract classes and in which conditions you will choose out of the two only one.. I have come up with my analysis for interface and that is...

Interface is best choice for Type declaration or defining contract between multiple parties. If multiple programmers are working in different modules of a project they still use each others API by defining interface and not waiting for actual implementation to be ready.

This brings us a lot of flexibility and speed in terms of coding and development. Use of Interface also ensures best practices like "programming for interfaces than implementation" and results in more flexible and maintainable code.

But I don't have strong reasons to justify the abstract classes, Please advise..!

Mistu4u
  • 5,132
  • 15
  • 53
  • 91
user1582269
  • 285
  • 1
  • 6
  • 12
  • Possible duplicate: http://stackoverflow.com/questions/2869222/use-of-java-interfaces-abstract-classes?rq=1 – blackcompe Aug 09 '12 at 18:23
  • Abstract classes have similar advantages. *Programming to an interface does not mean the same as using an `interface`*, it can be done with abstract classes as well. The main reason to use abstract classes is to provide some implementation that can be shared by all its subclasses. The main restriction here is the ability to only extend a single class at once. – toniedzwiedz Aug 09 '12 at 18:26

7 Answers7

30

Abstract classes are used to group a number of concrete classes under one entity.

For example, take the abstract class Animal. Animal is not something concrete. it's a family of, well, animals. but they all share certain aspectes, for example, each has a speak() option (well, except fish and sort). but each one implements it differently. this way you can override just the methods which are not the same, for example sleep() or breath() are common (again, fish are differnet :) ).

Interfaces on the other hand are more direct definition of an 'action'. That's why most (if not all) the interfaces in Java ends with 'able' (Comprable, Serializable...) By implementing the interface, you're telling other programmers or who ever uses your code that this class can do this and this. A dog, for example, is not, Animable.

Basically, to sum it up, I think that the best definition is this. Use abstract classes when you have a class that A is kind of B and interface when A can do B.

Hope that's help.

La bla bla
  • 8,558
  • 13
  • 60
  • 109
  • 1
    You stole my Animal example, but I really liked your closing sentence. +1 for concisely summing it up. – Petrucio Aug 09 '12 at 18:34
  • I'm pretty sure most interfaces in the Java library do not end `able`. "is kind of" vs "can do" strikes me as wordplay. – Tom Hawtin - tackline Aug 09 '12 at 18:38
  • 1
    @Petrucio, sorry. didn't see that. It's the example most widely used in my University. Tom Hawtin, I didn't understand what you're trying to say. as for the 'able' the most I've used do. Runnable, Comprable, Serializable. that's what I tried to convey. – La bla bla Aug 09 '12 at 18:48
5

Abstract classes are used when you want to provide partial implementation.

Lanaru
  • 9,421
  • 7
  • 38
  • 64
2
  • Abstract classes can have default behaviour(implementation) if is required, interfaces cannot.

  • An abstract class can provide default behaviour for ALL methods or no methods whereas interfaces cannot.

  • Abstract classes can have state shared with all subclasses, interfaces don't specify state.

  • You can implement multiple interfaces, you can only extend one (abstract) class.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

An interface declares a contract with whatever implements it. It's a guarantee that the class will contain the methods in the interface.

An abstract class is similar in that anything that subclasses it will also have to implement the abstract methods, but you can also have working methods with code in them.

I use interfaces a lot for callbacks with Android programming. Abstract classes I use a lot if I have a lot of similar data to display and just want small changes to implementation. Use an abstract class to cut down on repeated code while still having different implementation.

telkins
  • 10,440
  • 8
  • 52
  • 79
1

You are correct on your definition of interfaces.

Abstract classes should serve a completely different purpose. As an example, let's say you are implementing several classes for an animal simulator. 'Animal' defines several behaviours that could already have a basic implementation, but is not itself something that would make sense to have instantiated. Likewise for 'Mamal', a bubclass of 'Animal'. Only a subclass 'Tiger' would not be abstract, but most of what a tiger does that's not tiger specific would be implemented in it's abstract superclasses.

Petrucio
  • 5,491
  • 1
  • 34
  • 33
1

1. If speaking generally, An Abstract class will be needed when we need to force down some features on the Sub-Class from Super-Class, letting the Sub-Class to have the flexibility to add other functinality.

Eg:

Let Car be the Abstract Super-Class, which has 4 Tyres, 1 Steering ,etc...

Now the Sub classes like Santro i10, Maruti-800, Mahindr Bolero etc are Sub classes, but they need to have 4 Tyres, 1 Steering to be called a car, not they can have a radio or not as an additional feature.

2. Interface was introduced in java, because there is no Multiple Inheritance.

3. Interface is more about providing a role.

Eg:

Let Dog be the Super-Class.

Wild Dogs and Pet Dogs are Sub-Classes.

Wild Behavior and Pet Behavior are Interfaces

Now as both Wild Dog and Pet Dog are dogs, but with different behavious. Then they must implement the Wild Behavior or Pet Behavior respectively

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

you'd want to use an abstract class when you want your classes to only implement the methods if the situation would call for them.

an example would be if you had a super class vehicle and you wanted your sub-classes to give you number of wheels if they have them. You wouldn't need them if they were boats so the contract would force you to implement still while you could just out right ignore it for your abstract class.