Abstraction in Java
Want to learn more about why we need abstraction in Java?
Join the DZone community and get the full member experience.
Join For FreeIf you recently started learning Java, then I believe you must have somewhere come across a term called object-oriented programming or OOP. Now, there are four pillars in OOPs — abstraction, polymorphism, encapsulation, and inheritance. In this article, we will discuss one of the four pillars of OOP — abstraction.
Basically, abstraction is the art of hiding implementation details from the user and provide the user with what they want. Let’s try to understand that further with a real-world example. Most of us are quite fond of owning a car. When we go to place an order for the car, we are really not interested in understanding the fine details of the implementation of each and every component inside the car engine, glove box, etc. We leave those technical details and implementation for manufacturing engineers and mechanics to understand — we are simply interested in the car, not the manufacturing company. They are interested in providing us with what we want and hiding the finer implementation details. Likewise, there are tons of real-world examples where abstraction is at play — whether in the smartphone you are using or a smart television — all have implemented abstraction in one way or another.
Coming back to Java programming and any object-oriented programming method, the same principle follows for the code’s implementation details, which are hidden and only the necessary functionality is provided to the user. There are two ways to achieve abstraction in Java:
- By using interfaces
- By using abstract classes
Interfaces
Consider a television remote that only contains the functionality to operate a television, and it cannot serve any other purpose besides operating the television. For example, you won’t be able to operate a refrigerator with a television remote. Here, the remote acts as an interface between you and the television. It contains all the necessary functionalities that you require while hiding the implementation details from you. In Java, interfaces are similar to classes, except they contain empty methods and can contain variables. By empty methods, it means that they don’t provide any implementation details and it is for the classes or clients to provide the necessary implementation details for that method (or methods) when they implement the interface.
Syntax:
1.public interface XYZ {
public void method ();
2.}
Example:
1.public interface TelevisionRemote {
public void turnOnTelevision();
public void turnOffTelevision();
2.}
The Java class can use the implements
keyword to implement the interface and provide the implementation of the methods of the interface.
Example:
public class Television implements TelevisionRemote{
@Override
public void turnOnTelevision(){
//method implementation details
}
@Override
public void turnOffTelevision(){
//method implementation details
}
}
Interfaces provide contract specification or sets of rules for the classes implementing them. They set rules for classes and tell them what to do and what not to do. In case the class does not provide an implementation for all the methods of the interface, then the class must be declared abstract. We will cover the abstract classes later. They provide total abstraction, which means that all the methods are empty and field variables are public static and final by default. Interfaces serve several features:
1. They provide total abstraction.
2. They help to achieve what we call multiple inheritances as Java doesn’t support multiple inheritances, but you can implement several interfaces in one class, and thus, it helps to achieve multiple inheritances.
3. They help to achieve loose coupling in the design pattern implementation.
Abstract Classes
Abstract classes are just like a normal Java class, except they use the keyword abstract
in front of class declaration and method declaration.
Syntax:
public abstract class XYZ {
public abstract methodName();
}
Example:
public abstract class Automobile {
public abstract void engine();
public void gearBoxGearOne(){
//method implementation
}
}
Abstract classes are created using the abstract
keyword and they may or may not have method implementation. If a method is declared abstract, then its implementation has to be provided by the class extending the abstract class. We can have an abstract class without the abstract method. They can also contain final
methods. A class that extends the abstract class is a child class for that abstract class and has to provide an implementation for the abstract method declared in the abstract class.
Example:
public class Car extends Automobile{
@Override
public void engine(){
//Method implementation
}
}
Now, one question must have come up: Why do we have interfaces and abstract classes? There are a few key reasons worth noting:
1. Interfaces are implicitly abstract and cannot have implementations. Abstract classes can have method implementations.
2. Variables of interfaces are final by default. Abstract classes may or may not have a final variable.
3. Interface methods are public whereas abstract classes can provide all types of access modifiers for its members, i.e. public, protected, or private.
4. An interface can extend only while classes can implement multiple interfaces and extend one class only.
Thus, both abstract classes and interfaces are used to achieve abstraction and both have their own importance while designing a Java solution, but the most preferable choice for most developers is to use interfaces as they provide complete abstraction. I hope this article helps to clear up any questions or doubts regarding abstraction.
Published at DZone with permission of Chandu Siva. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Top 10 Pillars of Zero Trust Networks
-
Integrate Cucumber in Playwright With Java
-
Turbocharge Ab Initio ETL Pipelines: Simple Tweaks for Maximum Performance Boost
-
Top Six React Development Tools
Comments