Difference between Interface and Abstract class in Java
Difference between interface and Abstract class in Java is one of the most important interview question for freshers as well as for experienced candidate. and also one more question – when to use Interface and when to use Abstract class, explain with real life example.
Here, you will get to know the difference between these two and will also see the real life example of its uses.
Interface
- It is 100% incomplete.
- Every method is public and abstract(Incomplete).
- If we don’t know anything about implementation, just we have requirement specifications then we should go for an interface.
- Every variable inside the interface is public, static and final, whether we are declaring or not.
- We can not declare interface variables with private, protected, transient, volatile.
- We can not declare IIB and SIB blocks.
- Initialization is mandatory while declaring variables otherwise we will get compile time errors.
public interface iInterface {
void test();
public void test1();
int x; // Error - because intialization is mandatory
int y = 10; // This y is by default public static and final
public static final int z = 20;
private int p = 10; //ERROR - Illegal modifiers, only public is allowed.
{
System.out.println("Instance Initialization Block of Interface"); //ERROR - IIB is not allowed in Interface
}
static {
// SIB - runs only once when class is loaded
System.out.println("Static Initialization Block of Interface"); //ERROR - SIB is not allowed in Interface
}
iInterface(){
System.out.println("Constructor of Interface can not be created"); //ERROR - constructor of Interface can not be created.
}
}
Abstract Class
- It is 0 to 100% incomplete.
- Every method needs to be public and abstract. We can create concrete methods also.
- If we are talking about implementation but not completely then we should go for an abstract class.
- The variables inside abstract class need not be public, static and final.
- There are no restrictions on abstract class variable modifiers.
- We can declare IIB and SIB blocks.
- Initialization of variables is not mandatory at the time of declaration.
public abstract class aAbstract {
void test(); // ERROR - abstract keyword in mandatory to create incomplete method
abstract void test1();
public abstract void test2();
protected abstract void test3();
abstract void test4();
private abstract void test5(); // ERROR - private not allowed
public void test6(){
System.out.println("Complete method");
}
int s; // Initialization is not mandatory
private int x = 10;
public int y = 20;
public static int z = 30;
public static final int p = 40;
{
System.out.println("IIB of Abstract Class");
}
static {
System.out.println("SIB of Abstract class");
}
aAbstract(){
System.out.println("Constructor of Abstract Class");
}
}
When to use Interface and When to use Abstract class in Java
Let’s suppose we have two types of employee, Contractual and Permanent.
Scenario 1:
We know that for each type of employee we have to generate emails and also a manager has to be assigned. So we can create an interface where we can define generateEmail() and assignManager() abstract methods, so that it can be implemented individually in a derived class.
Scenario 2:
Let’s suppose the company has decided that the dress color should be BLUE for each employee whether they are permanent or contractual. But the dress code will be different for each employee. Then in this case we will go for an abstract class where we can define a complete/concrete method for dress color(i.e. dressColor()), and an incomplete/abstract method for dress code(i.e. dressCode()).
Public class PermanentEmployee{
}
Public class ContractualEmployee{
}
Public interface IEmployee{
Void generateEmail();
Void assignManager();
/* An interface can be a good choice when you know that a method has to be there but
it should be implemented individually in derived classes.
*/
}
Public abstract class EmployeeDress{
Public abstract void dressCode();
Public void dressColor(){
System.out.println(“BLUE”);
}
/*Abstract class can be a good choice when you are sure that some methods
are concrete and must be implemented in the same way in all the derived classes.
*/
}