What is Abstract Class and Interface in Java?
This article is going to be a brief one with some details about the interfaces and abstract classes in java. There has been a lot of confusion about using them interchangeably, but as I always say, encapsulation is a strong feature of java and one of the most important rules in java is that everything has its own purpose. Hence, if two different features (abstract class and interface) exists in the language, then they must have distinct reasons as well.
Point of the Article
The article narrates in detail using Abstract Classes And Interfaces, which is the dissimilarity between interfaces and abstract classes in terms of design and how a good API should differentiate between both of them. What should be your thought while writing an API? When You will be using ‘interface’ and when you will be using ‘abstract class.
Keep patience and read till the end if you are searching for an answer which you can give in interviews and impress the interviewers. Let us about abstraction before writing about the differences in this article. Before we start I would like you to have a glance at the image below and think for a while and try to answer the question below: What If I ask you to classify the objects in the image there can be many categories. Some of them are mentioned below
We can have a number of categories but for this discussion, we can be good with two. What exactly did we notice or classified? What is the basis of the classification?. I would say behaviors and I can also say capabilities. The first category is based on behavior and the
second category is more of a capability-based behavior. Now that we have some understanding of how to classify things, its time to present the term ‘Abstraction’. We must understand the difference between features, capability, and behavior. Capability is what an object can do. Behavior is what an object does feature is defined by the attributes of an object.
There is a strong feeling and we see that the objects in the first-class make more sense to be together, they seem to be indivisible or I may say closely related. Whereas the objects in the second class are more of a group and they need not exist closely.
In Java or Object-oriented programming language, abstraction is a way to separate implementation from an interface and one of the five core fundamentals along with, Inheritance, Polymorphism, Encapsulation, Class, and Object.
Abstraction in Java can be achieved by using interface and abstract class in Java. An interface or abstract class is something which we can say is not concrete, something which is incomplete.
if you want to use interface or abstract class, all you need need to do is extend and implement an abstract method with concrete behavior.
One example of Abstraction is creating an interface to indicate common behavior without specifying or implementing any details about how that behavior or method works. for example, you create an interface that has the name Server which has the start() and stops () method. This can be called an abstraction of Server class because every server can have a different to start and stop and details may differ from one another.
Abstraction in Java is can be implemented using abstract class and interface. In fact, what is an abstraction in Java?, How to achieve it? what is the difference between Abstract class and Interface is also a very popular core Java interview because strong OOPS skill is one of the key requirement to become Java developer?
Abstraction is also one of the hard to master the concept. The programmer often mixes different levels of abstraction which may lead n in-flexible code, so be careful while using the concept.
In Object-Oriented Concept Abstraction means”Exposing functionality and Hiding Implementation details”. This exactly means that we declare a behavioral aspect and features of an object kind which is yet not visible. Abstraction is a programming construct, which helps in abstracting the features/behavior of a given type of object to form a group. Let me try to define it in a simpler way: Let us say we are writing an application to manage vehicles. Now let us list what type of vehicles we can have, we can have cars, trucks, motorbikes, bicycles, aircraft, boats, etc. Moreover, if we closely observe none of them is a concrete instance, I will never say I have a car or a truck or a motorbike. I would rather say that I have a Car (C) which is of Brand Honda, red in color with registration number XYZ1234, etc. Basically, something which can uniquely recognize as the car (C). So the car (C) is a concrete instance of Vehicle class. Similarly, we can have many concrete instances for each of the class of vehicles. Where does abstraction fit in? As I mentioned above, I trust that abstraction must be used to present a feature or behavior, now we clearly notice that all the vehicles can have few features in common like they will have a color, an engine number, max speed and many more. Ways to use abstraction We can use Abstraction with the help of the following language features: Abstract classes Assume I ask you to create an Object of a Car, there is a chance that few of these pivotal features are missed if every individual is going to write an implementation. Also, I would like to force the user of my API(Application programming interface) to provide these common features of all the vehicles. This is to maintain a Familiar set of features across all the vehicles, this is where the need for the abstract class comes. We abstract the most common features to another class, also it is a good programming practice to have a special class for this purpose which would not make a logical sense if we create an object of it. For e.g, if we create Object a Vehicle it might not make much sense. But if we make Object of a Car class that makes more sense. So when we are writing an API we would like to use abstract classes for internal usage (within the API).
Interfaces are mainly used for the second part/categorization where a capability is to be presented. So interfaces clearly can be implemented with objects which are not closely related to each other. We can say, there is a sense of loose coupling when we think of interfaces. I must say that if we are trying to supply an API to the users(external usage) we would prefer to provide interfaces.
The Big Debate
Both Interface and Abstract classes are capable of providing behavior to the classes which extend or implements them then why not always use an interface because it gives more space to the implementing class to extend another class.
The answer can be slightly long. When we talk about providing behavior both interfaces and abstract classes are equally good to perform that, but when we talk about providing properties or attributes to the children, or maybe we can say features then abstract classes can do better than interface because the class extending it can get all the protected member variables from parent abstract class.
To use an interface we need some solid reason, if we are trying to supply an API to the users we will prefer to provide them an interface as interfaces are a just contract which defines that if some class is trying to implement an interface, they must provide an implementation of methods inside interface. For e.g. if someone is trying to implement a Flyable interface then everyone should provide an implementation of the fly method and should write down how should the implementing class ‘fly’.
A Note for distinguishing between abstract class and interface:
Classes are nouns like Actor, Vehicle, Object etc and Interfaces are Adverbs like Serializable, Cloneable, Flyable, etc.
See the word able that shows that it is to provide capabilities.
Here is a diagram to show to distinguish pictorially, Vehicle and Animal are abstract classes whereas Flyable is an interface. Please try to observe the difference in the lines going to an interface and the lines going to the abstract class.
The solid lines imply extension (inheritance by extending classes), for e.g Bird class, can extend Animal class. The dotted line implies implementation (inheritance by implementing interfaces), for e.g. Aircraft implements Flyable.
At last, I would like to highlight what I believe about abstraction. Abstraction is not just another feature of Object-Oriented Programming language, it is the result of one important feature i.e. Inheritance and it should be treated as a part of the inheritance and not just another feature because Inheritance is the reason why we have to use abstraction.
Here we learned how to make use of Abstract Classes And Interfaces, how and when it should be used and when to choose one over the other. We tried to develop a clear understanding of the reason behind having two different language constructs.
Shah Rukh Patwekar | Software Trainer
SevenMentor Pvt Ltd.
Call the Trainer and Book your free demo Class for JAVA now!!!
© Copyright 2019 | Sevenmentor Pvt Ltd.