Easiest explanation of Abstract class and Interface

This is perhaps the most asked question in any interviews related to Software development or software quality assurance. I have seen from my experience that often many of us get confused or forget the definition and usage of abstract classes and interfaces. So, I want to make it the easiest so that no one’s having any troubles with it ever again.

Abstract class and interfaces are two very vital pivots in the Object Oriented Programming concept.

At first, I will explain Abstract class and it’s usage, and later on we will move into Interfaces.

Abstract Class:

Abstract class is nothing special to look at when you want to differentiate with a regular class. But it has got some methods which are unimplemented, or only declared, not defined. In that case, all those methods and the class itself are renamed with the “abstract” keyword.
An abstract class can have one or multiple number of unimplemented methods.
As an abstract class is not proper, or does not have ideally defined methods compared to a regular class, so abstract classes cannot be instantiated, that means we cannot create any direct objects of abstract classes.
The classes which Inherit/Extend this abstract class, have to implement/define all the abstract methods of the parent abstract class.
If any class fails to define any of the abstract methods, that class also becomes an abstract class.

Usage (An imaginary case study):

Let us suppose, in 1995, you have designed a car class which had an additional feature-it could fly. You designed the car class very nicely, all your methods were very standard and efficient, but back then, you knew it was quite impossible to implement the carFlying() method. So, you just kept the carFlying() method blank, or unimplemented (Now you know that is an abstract method as well as class!).

Today, I want to implement that flying car that you have already designed and the technology is no longer impossible. The car I am gonna build has all the features similar to your car, and additionally it can fly. So, what can I do is I can just inherit (“extends”-for Java) your abstract class and must define/implement that carFlying() abstract method. here you go! our flying car is ready to roll.

So, whenever we have some common features or behavior among various objects, we can make a general abstract class with that feature as a method implemented. But those object might have distinct behaviors, in that case, we will declare an abstract method and will implement those abstract methods differently according to the object’s need.

Example of Abstract Class:

public abstract class Wrestler {

public void paymentForWork (int hours) {
System.out.println("The wrestler will make $"+hours*250.00+);
}
public abstract void themeMusic();
public abstract void finisher();
}

Once again, let us consider an example, a bunch of wrestlers get the same amount of money per hour for playing. but they have different theme music and finishing fight strategy. So, we can keep the payment method same for all and have it implemented in a class named Wrestler. lets declare the themeMusic() and finisher() methods as abstract because these two have to be implemented differently for different objects in some other classes which inherits our Wrestler Abstract class.

public class Kane extends Wrestler{

@Override
public void themeMusic() {
System.out.println("Kane's Intro music...");
}
@Override
public void finisher() {
System.out.println("Kane's finishing TombStone...");
}
}

and the main.java:

public class Main {
public static void main (String [] args) {
Wrestler wrestler1= new Kane();

wrestler1.themeMusic();
wrestler1.paymentForWork(5);
wrestler1.finisher();
}
}

Interface:

If you have gone through the upper part of this tutorial properly, understanding interface is going to be very easier for you.
Interface is not a class, its just interface. It has got some methods with no body inside it, just the method signature (they are basically abstract methods!), and may have some final and static variables. And, a typical class “implements” interfaces, not “extends”.
Multiple interfaces can be implemented (generally, inherited) at a time.
The classes which Inherit/implement the interfaces, must define all the abstract methods of the interface.
Methods inside interfaces are by default public, void and abstract.
interface is kind of multiple inheritance in Java programming language.
We cannot instantiate interfaces too just like abstract classes. Because they are incomplete entity, and should not be permitted to be realized. But sometimes an activity like instantiating can be applicable for it with the help of the anonymous inner class. That’s not our agenda here today.

Usage of Interface:

Interfaces save coding time. It can contain some static and final variables which can be accessed globally. and some methods which are abstract and readily available for further use anytime to whoever implements the interface.

Example of Interface:

In the above example of Abstract class, we saw that one method was same for all which had a body inside the Abstract class and other methods were abstract. but for Interfaces, all the methods are now just signature. The class StoneCold implements Wrestler interface and define all three abstract methods of it according to its need.

This time, different wrestlers get different amount of money per hour. So, its the most convenient to put the same method as abstract and define it elsewhere according to object’s need.

public interface Wrestler {

public abstract void paymentForWork (int hours);
public abstract void themeMusic();
public abstract void finisher();
}

and the following class implements the Wrestler interface.

public class StoneCold implements Wrestler {
@Override
public void paymentForWork(int hours) {
System.out.println("Stone cold will make $" + hours*300.00);
}
@Override
public void themeMusic() {
System.out.println("Stone cold's music playing...");
}

@Override
public void finisher() {
System.out.println("Stone cold's finishing stunner...");
}
}

For the main.java:

public class Main {
public static void main (String [] args) {
Wrestler wrestler2= new StoneCold();

wrestler2.themeMusic();
wrestler2.paymentForWork(5);
wrestler2.finisher();
}
}

For object initiating in case of Abstract class or interface, you can keep the following in mind,

A obj= new B();
//where A is an interface/abstract class. B is the concrete(implements/extends “A”)class which gives it’s memory to A’s obj to have access into B class and utilize its defined methods.

That was a lot of talk. I could not make it any shorter with proper explanation and examples. But I hope you have understood all of it and aren’t gonna forget at least in recent times! I do have all the references of information used here, knock me to get it.

If you have liked it, Please clap it and follow me. I must come back with some more tutorials soon. Have a good day :)