With my personal experience I can say, this is question has always been a favourite question for the interviewers. But there are very few online resources where you can find a proper reply for this though I have already published this article before in another blog.
Let’s just dig into Interface & Abstract class first.
What is an Interface?
An interface is a named collection of method definitions without implementations.
Interface is actually a template in Java. It contains only the method declarations. The class that implements the interface contains the definitions for the abstract methods of the interface. Interface can also contain fields, but the fields should be static & final. On the other hand, the abstract methods declared in the interface are abstract & public.
We need to remember some points regarding interface:
1. All the methods are abstract & public
2. All the fields are static & final.
3. Interface files always have .java extension although they are not classes.
4. Byte code of an interface always appears in a class file.
5. We can’t create object of an interface.
6. Unlike inheritance, a class can implement more than one interface.
7. One very important point – If a class is implementing an interface, the class will have to implement all the methods declared in the interface. Otherwise it will throw a compilation error.
8. Any number of methods & variables can be declared in an interface.
9. An interface can extend another interface like a class extends another class.
Now, these all the definitions are somehow a bit theoretical. We need to use interface practically. For that, we need an Interface, a class that implements the interface & a main method from which the caller class (which implements the interface can the called)
Example for simple interface implementation :
Class that implements interface interfaceTemplate:
The class which is accessing the interface interfaceTemplate using InterfaceCaller:
When you will run this in your machine, you are going to get this result:
Example for an interface extending another one:
Interface which extends SuperinterfaceTemplate
The class which is implementing both the interfaces above:
The class that has main method & accesses the interfaces:
When you are running this code in your machine you are going to get a result like this:
What is Abstract Class?
An abstract class is a class that is declared
abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated. Abstract classes can be accessed only by extending them.
There are some key features we should remember about abstract class:
1. All the methods in Abstract class may or may not be abstract. We can define methods inside an abstract class.
2. An Abstract class can’t be instantiated.
3. If any subclass inherits an abstract class, that contains abstract methods, then the subclass must implements all the abstract methods of the abstract class.
4. Just like normal Interface & classes, an abstract also can extend another abstract class.
Example for a simple abstract class implementation:
A class extending the abstract class
The main method :
Example for an abstract class inheriting another abstract class:
A simple class extending both the abstract classes
When an Abstract Class implements An Interface !!!
Although we have already told that if a class implements an interface, it will have to implement all the methods mentioned in the interface, it if not fully correct. If an abstract class implements an interface, it is possible that the abstract class may not define all the methods that are present in the interface.
When to use interface & when to use Abstract class ?
I am giving my personal opinion for this answer. Everyone, please feel free to share your opinion for this answer & correct me if am answering this question wrong.
In my opinion, when to use interface & when abstract class is purely requirement specific. For example, we need to implement some logic (we can take an example for color of car, internal amenities like A.C etc.) for any 4 wheeler that is produced by a certain company. So it is clear that if that company produces 2 different models of 4 wheelers, we have to write the logic 4 times for each model of 4 wheelers produced by that company.
Class implementing the interface for Model1:
Class implementing the interface for Model2:
A.C is not
Then, where we can use Abstract Class?
Abstract class can have abstract methods as well as method declarations in it & that’s the key of our answer. So, if we want to create some properties for those 2 models which are same for both are cars as well as some properties which are not same for the models, then we will use abstract class.
So, the abstract class defined is :
And, the classes that are extending the abstract class are:
Hope you all have enjoyed reading!!!
Please feel free to share your comments.
"Knowledge is knowing that we cannot know." - Ralph Waldo Emerson