In the previous tutorial, you have learned the difference between overloading and overriding in Java. In this tutorial, you will learn about the difference and use of Abstract class and Interface.
Abstract class and Interface OOP
Abstract Class:
A class is called abstract when it is declared as an abstract class using abstract keyword.
It’s not necessary to declare all the methods in the abstract class as abstract methods. But when a method is declared as an abstract method in abstract class then it has no body and the class which extends that abstract class must have to define that abstract method.
It is the same concept as we use virtual and pure virtual functions in C++ to make a class abstract.
Syntax:
1 2 3 |
abstract class Country{ abstract public void city(); } |
As in the above syntax abstract is a keyword in Java use to declare a class as abstract This class contain one abstract method city (). It is not a necessary to declare all methods abstract in an abstract class, but when some class extends that class, then it should implement all the abstract functions of the abstract parent class.
1 2 3 |
class Province extends Country{ void city(){ … } } |
The abstract class can inherit only one class or one abstract class at a time. It can have both abstract and concrete methods. In the abstract class, the keyword ‘abstract’ is compulsory to declare a method as an abstract. It can have protected and public abstract methods. It can have static, final, non-static or non-final variables with any access specifies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package abstractjava; abstract class Person{ void eat(){ System.out.println("This Person can eat"); } abstract void run(); } class Teenager extents Person{ void run(){ System.out.println("This perosn can run"); } } class mainclass{ public static void main(String args[]){ Teenager obj=new Teenager(); obj.run(); obj.eat(); } } |
Interface:
An Interface is a blue print of a class. It contains only abstract methods. Its members should not be static. It has no constructor. An interface is a reference type in Java, it is similar to class, and it is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Syntax:
1 2 3 4 5 |
interface Country { void city(); } |
Properties of Interfaces
Interfaces have the following properties:
- An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.
- Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
- Methods in an interface are implicitly public.
The class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration. Like above we create an interface name Country, now to implement the interface.
1 2 3 |
class Province implements Country { void city() {…} } |
The interface can extend any number of interfaces at a time. A class can implement more than one interface. Interfaces have only abstract methods. A class can implement any number of interfaces. In an interface keyword, ‘abstract’ is optional to declare a method as an abstract. The interface cannot have access specifiers, have only public abstract methods i.e. by default.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
//create interface package MyPack; interface Person{ public void eat(); public void run(); } //create class teenager to implement interface package MyPack; class Teenager implements Person{ @Override public void eat(){ System.out.println("This perosn can eat"); } @Override public void run(){ System.out.println("This perosn can run"); } } //create main package MyPack; public class Main{ public static void main(String Args[]) { Teenager obj; obj = new Teenager(); obj.run(); obj.eat(); } } |
Abstract class can also implement an interface
As you know that abstract classes can have abstract methods as well as concrete methods, so when an abstract class implements an interface it must override and implement all the methods of the interface. You can understand this concept through the example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
//Create interface package MyPack; interface Person{ public void eat(); public void run(); } //create abstract class to implement interface package MyPack; abstract class Teenager implements Person{ @Override public void eat(){ System.out.println("This perosn can eat"); } @Override public void run(){ System.out.println("This perosn can run"); } } //create main and implent un implemented methods package MyPack; public class Main{ public static void main(String Args[]) { Teenager obj; obj = new Teenager(){}; /*a subclas is required by compiler to implement all its methods that is why some methods could not be implemented*/ obj.run(); obj.eat(); } } |
One interface can extend another interface
One interface can also extend another interface, which means that two interfaces can have one or more functions in common. The example below explains the concept of interface extending another interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//create interface package interfaceextendsinterface; interface Person { void eat(); void run(); } //create another interface to extend interface package interfaceextendsinterface; interface Teenager extends Person{ } //Create class to implement second interface package interfaceextendsinterface; public class Man implements Teenager{ @Override public void eat(){ System.out.println("This perosn can eat"); } @Override public void run(){ System.out.println("This perosn can run"); } } //create main package interfaceextendsinterface; public class Main { public static void main(String[] args) { Man obj=new Man(); obj.run(); obj.eat(); } } |
Output:

Hope you like this tutorial, Stay tuned for more upcoming tutorials. Stay Blessed!
In the next tutorial, you will learn about the concept of Generalization and Aggregation in Java