0% found this document useful (0 votes)
59 views3 pages

What Is Abstract Class and Interface in Java?

This document discusses the differences between abstract classes and interfaces in Java. It explains that abstraction refers to exposing functionality while hiding implementation details. Abstract classes are used to define common features for related objects, while interfaces are used to define capabilities for loosely related objects. When designing APIs, abstract classes should be used internally to define common properties, while interfaces should be exposed externally to define contracts for implementing classes. The key difference is that abstract classes can provide protected member variables for subclasses, while interfaces only define method signatures that implementing classes must fulfill.

Uploaded by

Shah Rukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

What Is Abstract Class and Interface in Java?

This document discusses the differences between abstract classes and interfaces in Java. It explains that abstraction refers to exposing functionality while hiding implementation details. Abstract classes are used to define common features for related objects, while interfaces are used to define capabilities for loosely related objects. When designing APIs, abstract classes should be used internally to define common properties, while interfaces should be exposed externally to define contracts for implementing classes. The key difference is that abstract classes can provide protected member variables for subclasses, while interfaces only define method signatures that implementing classes must fulfill.

Uploaded by

Shah Rukh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

 

Interface | Abstraction |APIs 

What is Abstract Class and Interface in Java? 


This article is about Abstraction and interfaces java. There has been a lot of confusion about using them indistinguishable, but as we 
know encapsulation is a strong feature of java and one of the most important rules in Java is that everything has its own reason. 
Hence, if two 
different features (abstract class and interface) survive in the language, then they must've dissimilar reasons as well. 
 
Point of the Article The article narrates in detail using Abstract Classes And Interfaces, what 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 
 

 
 
 
 
 

Vehicles 
●​ Airplane 

● Tank 
● Bike 
● Truck 
● Bicycle 
● Boat 
● Car 
Flyable 
● ​Kite 

● Bird 
●Airplane 

 

 

 
 
 
 

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. 

 

 
 
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 
 
 
 
 

You might also like