Abstraction in Java
Abstraction in Java is a concept that allows you to hide the implementation details of an object or a class and only expose the essential features or behaviors that the user needs to interact with. Abstraction helps to reduce the complexity of the code and make it more readable and maintainable. Abstraction also enables you to achieve a higher level of modularity and reusability in your program.
There are two ways to achieve abstraction in Java: by using abstract classes and interfaces. An abstract class is a class that is declared with the keyword abstract
and may contain one or more abstract methods. An abstract method is a method that is declared without a body and must be overridden by a subclass that extends the abstract class. An abstract class cannot be instantiated directly, but it can have constructors and concrete methods. An abstract class can also implement an interface.
An interface is a collection of abstract methods and constants that define a common behavior or contract for a group of classes. An interface is declared with the keyword interface
and can be implemented by any class that wants to follow the contract. A class can implement multiple interfaces, but it must provide the body for all the abstract methods in the interfaces. An interface can also extend another interface.
Here is an example of abstraction in Java using an abstract class and an interface:
// An abstract class that represents a shape
abstract class Shape {
// An abstract method that calculates the area of the shape
abstract double area();
// A concrete method that prints the shape name
void printName(String name) {
System.out.println("This is a " + name);
}
}
// An interface that defines a resizable behavior
interface Resizable {
// A constant that represents the resize factor
double RESIZE_FACTOR = 1.5;
// An abstract method that resizes the shape
void resize();
}
// A concrete class that extends Shape and implements Resizable
class Circle extends Shape implements Resizable {
// A private field that stores the radius of the circle
private double radius;
// A constructor that initializes the radius
Circle(double radius) {
this.radius = radius;
}
// A method that overrides the area method from Shape
@Override
double area() {
return Math.PI * radius * radius;
}
// A method that overrides the resize method from Resizable
@Override
void resize() {
radius = radius * RESIZE_FACTOR;
}
}
// A main class that tests the abstraction concept
class Main {
public static void main(String[] args) {
// Create an object of Circle class
Circle c = new Circle(10);
// Print the name of the shape
c.printName("circle");
// Print the area of the circle
System.out.println("The area of the circle is " + c.area() );
// Resize the circle
c.resize();
// Print the new area of the circle
System.out.println("The new area of the circle is " + c.area());
}
}
The output of this program is:
This is a circle
The area of the circle is 314.1592653589793
The new area of the circle is 706.8583470577034