Difference Between Abstract class and Interface OOP

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:

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.

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.

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:

Properties of Interfaces

Interfaces have the following properties:

  1. An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.
  2. Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
  3. 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.

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.

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.

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.

Output:

interface and abstract class output
interface and abstract class 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

Leave a Reply

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