Abstract class Vs Interface

Difference between abstract class and interface

To understand difference between abstract class and interface, first, we have to understand what is an abstract class and an interface and what are the similarities.

Abstract class in plain terms is a class which contains one more abstract methods. Abstract method has no implementation, just a declaration. Sub-classes of abstract class are responsible for providing implementation of such methods. Example of an abstract class as follows

package Examples;

/**
 * Created by sangar on 22.10.17.
 */
public abstract class AbstractBicycle {
    //member fields
    protected int gears;
    protected int topSpeed;
    protected int serviceCost;

    public int getGears(){
        return this.gears;
    }

    public abstract void service();
}

 

On the other hand, interfaces are like contracts which are honored by every class which implements them. It does not matter how a class implement the contract, as far as it implements it. For example, Television box can have may functionalities like changing channels, volume control, records, selection of source etc. TV manufacturers defined this industry standard interfaces, so that remotes can be build to call these behaviors on TV sets. It does not matter how different models of TV implement these interface, as far as they implement it.

Interface is again just like a class, which contains all abstract methods.  Implementation of those methods is left to class  which implements interface. Interface can contain constants too which are by default final. Example of declaration of an interface be

package Examples;

/**
 * Created by sangar on 22.10.17.
 */
interface IBicycle {

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}

So, what’s the difference between abstract class and interface then? There are very subtle differences as follows

  1. Abstract class is object oriented in nature as it defines hierarchy between classes, on the other hand interfaces are functional in nature as they define what a class has to implement to satisfy this interface.
  2. Methods and fields in abstract class can have individual access specifiers like private, public or protected, where as in interfaces, everything has to be public. Sub-class implementing abstract methods from abstract class can keep visibility same or reduce it, whereas class implementing interface has to keep visibility same which is public.
  3. A sub-class or child can only extend single abstract class, whereas a class can implement multiple interfaces.
  4. By definition, abstract class can have default implementation of some methods, where as interface just has definition of all methods. This is not true from Java 8 default method signatures in interface. Refer here for more details.
  5. Sub-class uses extends to use abstract class where implements to implement an interface.
  6. Abstract classes can have constructors but interfaces can’t have constructors.

Abstract class Vs Interface : When to use what?

Now, when to use what? When to use abstract class and when to go to interfaces.

Consider using abstract classes if any of these statements apply to your situation:

  • You want to share implementation among several closely related classes.
  • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
  • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

Consider using interfaces if any of these statements apply to your situation:

  •  If unrelated classes would implement your interface.
  • Specify the behavior of a particular data type, but not concerned about who  implements its behavior.
  • You want to take advantage of multiple inheritance of type.

Reference : Oracle documentation

In short, abstract class provides is-a kind of relationship between class and concrete sub-classes whereas interface provides has-a capability between unrelated classes.

This post clarifies difference between abstract class and interface and explains when to use what. Hope it helped you!

Please share if there is something wrong or missing. If you want to contribute to website, please reach out to us through contact form or drop a mail to jitsceait@gmail.com.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s