Introduction
In this article, we will learn about Abstract class and Interface in the Java programming language. Abstract class and interface are used for abstraction. Both the Abstract class and the interface can not be initiated.
Abstract class
In Java, when we use an abstract keyword with any class, this class is called an abstract class. An abstract class can have variables, an abstract method, a method body, a constructor, and main () method. Here are some important points about the Abstract class.
- An abstract class can have abstract methods and non-abstract methods.
- If a class has an abstract method, this class must be an abstract class.
- If we want to use an abstract class, it needs to be extended and its method implemented.
- If we extend an abstract class, we have to provide an implementation for all the abstract methods in it.
- We can’t instantiate an abstract class.
The complete program of an abstract class is listed below.
abstract class AbstractClass {
abstract void display();
}
public class AbstractClassexample extends AbstractClass {
void display() {
System.out.println("Abstract class example");
}
public static void main(String args[]) {
AbstractClass p = new AbstractClassexample();
p.display();
}
}
The above program generates the following output.
Interface
An interface in Java is similar to a class, and we can say that it is a blueprint of a class. Here are some important points about the Abstract class.
- The interface is mainly used to achieve full abstraction in Java.
- The interface has static constants and an abstract method body only and no method body.
- Interface supports multiple inheritance in Java.
- An interface shows the IS-A relationship also.
- Interface can’t be instantiated, as in the case of an abstract class.
In Java, the compiler, by default, adds public and abstract keywords before the interface method and public, static, and final keywords before the variables. It means that all the variables of an interface are public, static, and final, and methods are public and abstract.
In Java, Multiple inheritances are not supported through the class. It is possible through an interface because of the interface methods implementation provided by the other class. Hence, there is no ambiguity.
The complete program of the interface is listed below.
Step 1. First, we create a Java interface InterfaceExample in which we declare the method display():
public interface InterfaceExample {
public void display();
}
Step 2. Second, we create a Java class PublicExample in which we declare the method display():
public class InterfaceAccess implements InterfaceExample{
@Override
public void display() {
System.out.println("Interface class access the interface named InterfaceExample");
}
public static void main(String args[]) {
InterfaceAccess i = new InterfaceAccess();
i.display();
}
}
The above program generates the following output.
Differentiation between abstract class and interface in Java
Abstract class |
Interface |
An abstract class can hold abstract methods and non-abstract methods. |
The interface has only abstract methods. |
Abstract keyword is used to create an abstract class. |
The interface keyword is used to create the interface. |
Abstract class can contain static, non-static, final, and non-final data members. |
The interface has only static and final data members. |
An abstract class can contain static methods, the main method, and constructors. |
Interface can’t contain static methods, main methods or constructors. |
An abstract class can give the implementation of an interface. |
Interface can't give the implementation of an abstract class. |
An abstract class can extend only one class at a time. |
The interface can extend any number of interfaces at a time. |
An abstract class is used to achieve partial abstraction. |
The interface is used to achieve fully abstraction. |
An abstract class can extend from a class or from an abstract class. |
The interface can extend only from an interface.
|
For example
public abstract class Car{
public abstract void run();
}
|
For example
public interface Bike{
Void run();
}
|
Similarity between Abstract class and Interface
- Both an abstract class and an interface are used for abstraction in Java.
- We can’t instantiate both.
The complete program of abstract class and interface.
Step 1. Creating an interface that has 4 methods.
public interface Shape {
void draw();//bydefault, public and abstract
void print();
void show();
void display();
}
Step 2. Creating an abstract class that provides the implementation of one method of shape interface.
abstract class Circle implements Shape {
public void print() {
System.out.println("Print image");
}
}
Step 3. Create a subclass of an abstract class. Now, we need to provide the implementation of the rest of the methods
class Rectangle extends Circle {
public void draw() {
System.out.println("Draw image");
}
public void show() {
System.out.println("Show image");
}
public void display() {
System.out.println("Display image");
}
}
Step 4. Creating an Exampletest class that calls the methods of shape interface.
public class ExampleTest {
public static void main(String args[]) {
Shape a = new Rectangle();
a.draw();
a.print();
a.show();
a.display();
}
}
The above program generates the following output.
Summary
In this article, we learned about abstract class and interface in Java programming language and how these are different from each other.