Difference between Interface and Abstract class

Published by Manish Rawat on

Today we will discuss about interface and Abstract class. It is most important question for java interview also. what is Interface and Abstract class, when will need to use Interface or Abstract class. so let’s start.

What is Interface?

An interface is just like Java Class, but it only has static constants and abstract method. Java uses Interface to implement multiple inheritance. A Java class can implement multiple Java Interfaces. All methods in an interface are implicitly public and abstract.

Syntax for Declaring Interface

interface {
//methods
}
why is an Interface required?

java does not support multiple inheritance. When one class extends more than one classes then this is called multiple inheritance. For example: Class C extends class A and B then this type of inheritance is known as multiple inheritance. Java doesn’t allow multiple inheritance. 

but we can achieve this using Interface.To use an interface in your class, append the keyword “implements” after your class name followed by the interface name.

Example for Implementing Interface

class Cat implements Pet{
      //methods          
} we can achieve multiple inheritance using interface. class Cat implements Pet,Animal{
     //methods         
}
What is Abstract class?

A class that is declared using “abstract” keyword is known as abstract class. It can have abstract methods as well as concrete methods.
Example for Implementing Abstract class

//Declaration using abstract keyword
abstract class A{
//This is abstract method
abstract void myMethod();

//This is concrete method with body
void anotherMethod(){
//Does something
}
}

Difference between Interface and Abstract class

Serial no Interface Abstract class
1. If we don’t know anything about implementation just we have requirement specification, then we should go for interface. If we are taking about implementation but not completely or partial implementation than we should go for Abstract class.
2. Inside interface every method is always public and abstract whether we are declaring or not.
Hence interface is also considered as 100% pure abstract class.
Every method presents in abstract class need not be public and abstract. In addition to abstract methods we can take concrete methods also.
3. We can’t declare interface method with the following modifiers.
Public –>private, protected.
Abstract –> final, static, synchronized, native, strictfp.
There is no restriction on Abstract class method modifiers.
4. Every variable presents inside interface is always public, static and final. Whether we are declaring or not. The variables present inside Abstract class need not be public, static and final.
5. We can’t declare interface variables with the following modifiers like private, projected, transient, volatile. There is no restriction on Abstract class variable modifiers.
6. For interface variable compulsory we should perform initialization at the time of declaration otherwise we will get compile time error. For Abstract class variable it is not required to perform initialization at the time of declaration.
7. Inside interface we can’t declare instance and static blocks. Otherwise we will get compile time error. Inside Abstract class we can declare instance and static blocks.
8. Inside Interface we can’t declare constructors. Inside Abstract class we can declare constructor which will be executed at the time of child object creation.

I hope, you understood all the concept of Interface and Abstract class.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *