Abstraction in Java
In this tutorial, we are going to discuss abstraction in Java. Abstraction is the process of showing only the required information to the user by hiding other details.
For example, consider a mobile phone. We know that pressing the call button will dial the required number. But we don’t know the actual implementation of how a call works. This is because the internal mechanism is not visible to us. This is a real example of abstraction. Similarly, abstraction works in the same way in Object-oriented programming.
Use of Abstraction
- Hiding implementation details is nothing but abstraction. The main advantages of abstraction are we can achieve security as we are not highlighting internal implementation.
- Enhancement will become easy. With out effecting outside person we can change our internal implementation.
Ways to achieve Java Abstraction
Abstraction in Java is a fundamental concept in object-oriented programming (OOP) that allows you to hide the implementation details of a class and only show the necessary features of an object. This is achieved through abstract classes and interfaces.
Abstract Classes
An abstract class is a class that cannot be instantiated on its own and may contain abstract methods (methods without a body). Abstract classes are meant to be subclassed, and they provide a way to define a common interface for a set of related classes while leaving the implementation of some methods to the subclasses. Abstract classes can also have concrete methods.
Example
abstract class Shape {
// Abstract method (no implementation)
abstract void draw();
// Concrete method
void display() {
System.out.println("This is a shape.");
}
}
Interfaces
An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Methods in an interface are implicitly abstract and public. Interfaces provide a way to achieve multiple inheritance in Java and define a contract that implementing classes must adhere to.
Example
interface Animal {
void eat();
void sleep();
}
Classes that implement this interface must provide concrete implementations for the eat()
and sleep()
methods. You can refer to the Interface tutorial to understand more about achieving abstraction using an interface.
Abstraction allows for loose coupling and high cohesion in your code, making it easier to maintain and extend. It promotes a modular design where each component is responsible for a specific task without being concerned about the internal workings of other components.
Note
- If we don’t know about implementation, we have to represent the specification, and then we should go for an interface.
- If we don’t know about complete implementation, we have a partial implementation, and then we should go for abstract.
- If we know complete implementation and are ready to provide service, we should go for a concrete class.
That’s all about Abstraction in Java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!