Deep dive into Abstract Class vs Interface
Abstract class
what is an abstract class?
A class that is not complete is abstract. "Not complete" means methods or functions inside this class that don't have a body or definition.
Limitation of abstract class
we can't create objects of an abstract class(instantiation is not possible).
How can we specify a class as an abstract class?
Using the abstract keyword.
How can we restrict Object creation to any class?
By using abstract classes.
What is the purpose of a concrete class or normal class when it is declared abstract?
To promote an "is-a" relationship(meaning to promote inheritance).
Program for Abstract class
In the above pic, we have a class car that has methods such as mileage, cost and an abstract method speed. Here the mileage and cost methods have a body and speed doesn't have a body so we specify that as an abstract method and since the class is also abstract we cannot create objects of the Car class. So now if we want to print anything from mileage, cost and speed methods we cannot create objects of the class car to access them so we create a new class Maruthi and Skoda which extends the Car class and provide a definition or body to the Speed method and can also have access to the other methods in the Car class.
Abstract class vs concrete class(normal class)
Interface
What is an interface?
It is a blueprint or template or specification of a method prototype.
all methods of interfaces are public and abstract.
Why the methods of an interface are public and abstract by default?
they are public because they should be available to third-party vendors to provide implementations.
they are abstract because their implementation is left to the third-party vendor.
Declaration and implementation of an interface
in the above pic we can see that the Class Sample is Abstract, why is that?
It is Abstract because even though the Sample class implements ABC interface it has only provided the definition or the body to only the m1 method and there is no implementation of the m2 method in the Sample class. When a class implements an interface it has to override or provide a definition to all the methods in the interface and if it doesn't then that class is abstract.
why have we made the method in the sample class public?
Since we know that all methods in an interface are public and abstract by default. When a class implements this interface the methods we use in the class(methods that come from the interface)should have the same signature as it has in the interface( which is public in nature).
Note
Extends vs implements
example - class extending any number of interfaces
Interface methods
Interface variables
Abstract vs Interface
Subscribe to my newsletter
Read articles from Kishore Ramesh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Kishore Ramesh
Kishore Ramesh
I am a Software Engineer Trainee at Valuemomentum and I am interested in learning Fullstack development and keen on learning new technologies and document everything I learn in my blogs.