Interfaces and Abstract Classes in JAVA
Authors: Atharva Sawaleshwarkar, Mohiddin Shikalgar, Tanmay Shah, Farhan Shaikh, Yash Shejwal

When we are learning java and its concepts then we definitely came across the concept called Interfaces. Interfaces are one of the key features of JAVA and every java developer should know its use and applications. Interfaces come into the picture with their many advantages and disadvantages.
Let’s dive into the understanding interfaces. When implementing interfaces, we definitely come across terms called abstract classes. Now, what are abstract classes? Why they are needed? What is the interface? What are the uses of Interfaces? Why do interfaces use abstract classes in them? You will get all answers in this blog post.
What are Interfaces?
The interface is a special mechanism in java that helps to achieve abstraction. Actually, the Interface is a blueprint of a class. It has static constants and abstract methods. Interfaces can only have abstract methods(only methods with no body).
But currently, with java 9, we can also use private, default, and static methods in interfaces.
Let’s ignore technical words and go to simple logic to understand the interface which is used to achieve abstraction.
What is Abstraction?
Let’s take a real-life example, We all use apps on our mobile phones. Whenever we want to use any app we have to create an account in it. During signup, when we register with our phone number, OTP comes to our mobile.
Now here is an answer to abstraction, we know OTP comes after clicking the “Send OTP” button in-app but we don’t know how that system is working in the backend. After clicking the button what’s actually happening. This behavior of completing tasks successfully without displaying to the user what actually happening in the backend is known as Abstraction.
We can achieve abstraction using interfaces and abstract classes in java.
WHY DO USE AN INTERFACE?
There are mainly three reasons to use the interface. They are given below.
- It is used to achieve abstraction.
- By interface, we can support the functionality of multiple inheritances.
- It can be used to achieve loose coupling.

How to use an interface?
The interface is declared by using the ‘interface’ keyword. It provides abstraction means it declares the structure of the class. All methods in an interface are abstract and by default are set as public, static, and final. Whichever class is implementing the interface must implement all the methods declared in an interface.
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
Similar to interfaces abstraction can be achieved using abstract classes also.
What are Abstract Classes?
Abstract classes are classes with an ‘abstract’ keyword before them. They contain abstract as well as concrete(methods with the body) methods. Abstract classes can not be instantiated, they need to be extended and their methods implemented.
Points to Remember
- An abstract class must be declared with an abstract keyword.
- It can have abstract and non-abstract methods.
- It cannot be instantiated.
- It can have constructors and static methods also.
- It can have final methods which will force the subclass not to change the body of the method.

Example of an Abstract class that has an abstract method:
In this example, Bike is an abstract class that contains only one abstract method run. Its implementation is provided by the Honda class.
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
The abstract class having constructor, data member, and methods:
An abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.
//Example of an abstract class that has abstract and non-abstract methods
abstract class Bike{
Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear(){System.out.println("gear changed");}
}
//Creating a Child class which inherits Abstract class
class Honda extends Bike{
void run(){System.out.println("running safely..");}
}
//Creating a Test class which calls abstract and non-abstract methods
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();}
}
Now the main question arises, interfaces and abstract classes both help in abstraction but which one we should use more?
Java doesn’t support multiple inheritances as c++ does. We can achieve multiple inheritances using interfaces.
For abstraction:
Abstract Classes help = 1 to 100%
Interfaces help = 100%
Java Interface Example
In this example, the Drawable interface has only one method. Its implementation is provided by Rectangle and Circle classes. In a real scenario, an interface is defined by someone else, but its implementation is provided by different implementation providers. Moreover, it is used by someone else. The implementation part is hidden by the user who uses the interface.
//Interface declaration: by first user
interface Drawable{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class Circle implements Drawable{
public void draw(){System.out.println("drawing circle");}
}
//Using interface: by third user
class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()
d.draw();
}}
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritances.
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
Q) Multiple inheritance is not supported through class in java, but it is possible by an interface, why?
As we have explained in the inheritance, multiple inheritance is not supported in the case of class because of ambiguity.
However, it is supported in the case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.
We hope you understood the concept behind interfaces and why they are used?
Leave a comment and share your thoughts.
Reference: