Abstraction is a fundamental concept in object-oriented programming that allows us to focus on the essential features of an object and ignore its non-essential details. In Java, abstraction can be achieved through abstract classes and interfaces.
An abstract class is a class that cannot be instantiated and can only be used as a base class for other classes. It can contain abstract methods, which are methods that have no implementation and are intended to be overridden by subclasses. An abstract class can also contain concrete methods, which are methods that have an implementation and can be inherited by subclasses.
Here is an example of an abstract class in Java:
public abstract class Shape {
public abstract double getArea();
public abstract double getPerimeter();
public void printDetails() {
System.out.println("Area: " + getArea());
System.out.println("Perimeter: " + getPerimeter());
}
}
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
public class Main {
public static void main(String[] args) {
Rectangle myRectangle = new Rectangle(2.0, 3.0);
myRectangle.printDetails();
}
}
In this example, the Shape class is an abstract class that defines two abstract methods, getArea and getPerimeter, and a concrete method, printDetails. The Rectangle class is a subclass of the Shape class that overrides the getArea and getPerimeter methods. The main method creates an object of the Rectangle class and calls the printDetails method, which calls the getArea and getPerimeter methods. The output is:
Area: 6.0
Perimeter: 10.0
An interface is a collection of abstract methods that defines a contract for a class to implement. It can also contain static methods and default methods, which have implementations and can be inherited by implementing classes.
Here is an example of an interface in Java:
public interface Shape {
double getArea();
double getPerimeter();
}
public class Rectangle implements Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
}
public class Main {
public static void main(String[] args) {
Shape myShape = new Rectangle(2.0, 3.0);
System.out.println("Area: " + myShape.getArea());
System.out.println("Perimeter: " + myShape.getPerimeter());
}
}
In this example, the Shape interface defines two abstract methods, getArea and getPerimeter. The Rectangle class is a class that implements the Shape interface and provides implementations for the getArea and getPerimeter methods. The main method creates an object of the Rectangle class and assigns it to a variable of the Shape type. The getArea and getPerimeter methods are called on the variable, and the output is:
Area: 6.0
Perimeter: 10.0
In conclusion, abstraction is a powerful