Difference Between Interface and Abstract class in Java with Examples
Both Interface and abstract class in Java can contain abstract methods. The use of abstract methods is to achieve abstraction in Java.
The differences between interface and abstract class in Java are explained in the table below:
Interface | Abstract class |
---|---|
The interface keyword is used to declare an interface. | The abstract keyword is used to declare an abstract class. |
Interface can only have abstract methods. Since Java 8, interface can also contain default and static methods. | Abstract class can contain both abstract and non-abstract methods. |
It supports multiple inheritance. | It doesn’t support multiple inheritance. |
An Interface cannot have a constructor. | An Abstract class can have any number of constructors. |
By default any methods declared in the interface are public abstract. | It can contain any type of method. There is no restriction on abstract class method modifiers. A method doesn’t have to be a public abstract. It can be private, protected, etc. |
By default variables are public static final in the interface. Interface can only contain static final variables. | Abstract class can contain static, non-static, final and non-final variables. |
A class can implement an interface by using the implements keyword. | A class can be extended from an abstract class by using the extends keyword. |
Java Interface Example
Create the following Shape interface:
Shape.java
public interface Shape {
//By default this variable is public static final
String name = "Shape";
//By default this method is public abstract
void draw();
}
Lets create the following classes to implement the above Shape interface:
Circle.java
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing Circle");
}
}
Square.java
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Drawing Square");
}
}
Triangle.java
public class Triangle implements Shape {
@Override
public void draw() {
System.out.println("Drawing Triangle");
}
}
Example.java
public class Example {
public static void main(String[] args) {
//Calling Shape interface variable
System.out.println(Shape.name);
//Creating instances from implemented classes
Shape circle = new Circle();
Shape square = new Square();
Shape triangle = new Triangle();
//Calling methods
circle.draw();
square.draw();
triangle.draw();
}
}
Output:
Shape
Drawing Circle
Drawing Square
Drawing Triangle
Drawing Circle
Drawing Square
Drawing Triangle
Java Abstract Class Example
Lets create a Vehicle abstract class with abstract and non-abstract methods:
Vehicle.java
public abstract class Vehicle {
// abstract method
public abstract String getModelName();
public abstract void start();
public abstract void run();
// Non-abstract method
public void stop(Vehicle vehicle) {
System.out.println("Stopping " + vehicle.getModelName());
}
}
Lets create subclasses from the above Vehicle abstract class by using the extends keyword:
Toyota.java
public class Toyota extends Vehicle {
@Override
public String getModelName() {
return "Toyota";
}
@Override
public void start() {
System.out.println("Start Toyota");
}
@Override
public void run() {
System.out.println("Run Toyota");
}
}
Honda.java
public class Honda extends Vehicle {
@Override
public String getModelName() {
return "Honda";
}
@Override
public void start() {
System.out.println("Start Honda");
}
@Override
public void run() {
System.out.println("Run Honda");
}
}
Example.java
public class Example {
public static void main(String[] args) {
//Creating Vehicle instance of Toyota type
Vehicle vehicle1 = new Toyota();
vehicle1.start();
vehicle1.run();
vehicle1.stop(vehicle1);
//Creating Vehicle instance of Honda type
Vehicle vehicle2 = new Honda();
vehicle2.start();
vehicle2.run();
vehicle2.stop(vehicle2);
}
}
Output:
Start Toyota
Run Toyota
Stopping Toyota
Start Honda
Run Honda
Stopping Honda
Run Toyota
Stopping Toyota
Start Honda
Run Honda
Stopping Honda