Abstract class and interface both are used to achieve abstraction
in java. Still there are lot of differences between abstract class and
interface which I am sharing in this tutorial.
in java. Still there are lot of differences between abstract class and
interface which I am sharing in this tutorial.
Difference between Abstract Class and Interface in Java
Difference between Abstract Class and Interface in Java – Image Source |
Abstract
Class |
Interface
|
Abstract class can have both abstract and non-abstract methods.
|
Interface can have only abstract methods.
|
Abstract class is extended using “extends” keyword.
|
Interface is implemented using “implements” keyword.
|
By default the access specifier of methods is “default”.
|
By default the access specifier of methods is “public”.
|
Abstract class can have static, non-static, final and non-final variables.
|
Interface can have final and static variables.
|
Abstract class have constructor.
|
Interface don’t have constructor.
|
Abstract class is partial implementation of abstraction.
|
Interface is fully implementation of abstraction.
|
Multiple inheritance can’t be achieved using abstract class.
|
Multiple inheritance can be achieved using interface.
|
“abstract” keyword is used to declare an abstract class. An example
is given below.
abstract MyClass
{
public abstract void show();
}
|
“interface” keyword is used to declare an interface. An example is given
below.
interface MyInterface
{
void show();
}
|
This was all about difference between abstract class and
interface in java. If you found anything incorrect or have doubts regarding
above tutorial then please mention it.
You forgot the most important difference: extending an interface will break all implementations* while you can freely add new (non-abstract) methods to abstract classes.
* not counting java8 and interfaces with "default implementation" which makes them like abstract classes