10 Differences between Interface and abstract class in java - in detail with programs



Interface and abstract class forms base of core java. It’s very important to differentiate between two. We must be able to decide which must be used in real time. This is most prominently asked interview question.






Interface
Abstract class
1
Interface helps in achieving pure abstraction in java.
Abstract class aren’t purely abstraction in java
2
All Interface are abstract by default.
So, it’s not mandatory to write abstract keyword with interface.

Example-
interface MyInterface {//compiler will add  abstract
}

Because of default additions done by compiler, above code will be same as writing below code-

abstract interface MyInterface {
}

It’s mandatory to write abstract keyword to make class abstract.


Example-

abstract class MyAbstractClass{
  
   abstract void m();
  
}
3
All the variables in Interface are public, static and final by default. Interface variables are also known as constants.


interface MyInterface {
int i=2;//compiler will add public, static and final   
}

Because of default additions done by compiler, writing above code will be same as writing below code-

interface MyInterface {
   public static final int i=2;
}

Abstract class can have private, instance variables as well.
4
All the methods in Interface are public and abstract by default.

interface MyInterface {
void m(); //compiler will add public and abstract
}

Because of default additions done by compiler, writing above code will be same as writing below code-

interface MyInterface {
 public abstract void m();
}

Abstract class can have private, final, abstract, static and instance methods.

Note : Method cannot be private and abstract.
Method cannot be final and abstract.



5
Interface does not have constructors.

Abstract class have constructors.

Constructors are called by constructors of sub-class when object of subclass is created.



6
Interface allows us to achieve multiple inheritance in java.

Example - See Program 2.

Also see Program 2.1 to find out why interface allow multiple inheritance.
Abstract class  doesn’t allows multiple inheritance in java.



see Program 2.2 to find out why classes doesn’t allow multiple inheritance.
7
Interface doesn’t extend classes.
classes implement Interface.
classes can implement more than one interface.



8
If any new method is added in Interface then all concrete classes which implements that interface must provide implementation of newly added method, because all methods in interface are abstract by default.








Example -
Please see Program 3
If any new instance method is added in Abstract class then all concrete classes which extends that abstract class need not to provide implementation of newly added instance method..

If any new abstract method is added in Abstract class then all concrete classes which extends that abstract class must provide implementation of newly added abstract method.


Example -
Please see Program 4 and Program 5
9
Interface methods cannot have synchronized method, concrete class which implements interface can make methods synchronized.
Abstract class can have synchronized instance methods,
but
abstract methods cannot be synchronized.
10
When to use interface practically -
Let’s say we have to choose between class or interface for Animals, than habitat of  animals might be land or water.
And food of all animals might be different.



So, we will create interface with -
 abstract method = habitat()   [because animals might be living on land or water]
 abstract method = food()   [because food of all animals might be different]







Example -
When to use abstract class practically -
Let’s say we have to choose between class or interface for TerrestrialAnimals, than one thing will be for sure that habitat of all Terrestrial animals must be land. That means we can have same implementation of habitat method for all Terrestrial animals. And food of all Terrestrial animals might be different.

So, we will create abstract class with -
 instance method = habitat()   [because all Terrestrial animals live on land]
 abstract method = food()   [because food of all Terrestrial animals might be different]

instance method habitat() will be inherited in all subclasses.

Example -


/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

RELATED LINKS>

Interface in java - Multiple inheritance, Marker interfaces, When to use interface practically, 12 features

Abstract class in java - When to use abstract class or interface practically, 10 features


Must read for you :

No comments:

Post a Comment