Thursday, July 14, 2016

Difference between abstract and interface

 interfaces when you want a full implementation and use abstract classes when you want partial pieces for your design (for reusability)


1) Abstract class can have abstract and non-abstract methods.
Interface can have only abstract methods.

2) Abstract class doesn't support multiple inheritance.
Interface supports multiple inheritance.

3) Abstract class can have final, non-final, static and non-static variables.
Interface has only static and final variables.

4) Abstract class can have static methods, main method and constructor.
Interface can't have static methods, main method or constructor.

5) Abstract class can provide the implementation of interface.
Interface can't provide the implementation of abstract class.

6) The abstract keyword is used to declare abstract class.
The interface keyword is used to declare interface.

7) Example:
public abstract class Shape{
public abstract void draw();
}

Example:
public interface Drawable{
void draw();
}

Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully abstraction (100%).

Variables in interface are public static final. But abstract class can have other type of variables like private, protected etc
Methods in interface are public or public static but methods in abstract class can be private and protected too
Use abstract class to establish relation between interrelated objects. Use interface to establish relation between unrelated classes.
Have a look at this article for special properties of interface in java 8. static modifier for default methods in interface causes compile time error in derived error if you want to use @override.

This article explains why default methods have been introduced in java 8 :  To enhance the Collections API in Java 8 to support lambda expressions.


You want to share code 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:

You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
You want to 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.

public interface Actor{
   Performance say(Line l);
}
public interface Director{
   Movie direct(boolean goodmovie);
}

public interface ActorDirector extends Actor, Director{
...
}

No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...