What Is Abstract Class and Interface in Java?
What Is Abstract Class and Interface in Java?
Vehicles
● Airplane
● Tank
● Bike
● Truck
● Bicycle
● Boat
● Car
Flyable
● Kite
● Bird
●Airplane
1
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 behaviour. 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. Behaviour 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.
Abstraction
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 willnever 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 colour 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 vehicle can have few features in common like they will have a colour,
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
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.
2
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’.
Tags:
● Work with Java
APIs
● Interface in Java
● Abstraction in Java