In Java, an abstraction class is typically an abstract class that serves as a blueprint for other classes to inherit from.
Abstraction classes are used to define common characteristics and behaviours that subclasses can inherit and implement.
They often contain one or more abstract methods, which are methods without a body, meant to be implemented by subclasses.
Here's an example of an abstraction class in Java:
// Abstraction class abstract class Shape { // Abstract method for calculating area abstract double calculateArea(); // Abstract method for calculating perimeter abstract double calculatePerimeter(); // Concrete method for displaying shape information void display() { System.out.println("This is Called From Shape."); } } // Concrete subclass implementing the Shape abstraction class Circle extends Shape { double radius; Circle(double radius) { this.radius = radius; } // Implementation of abstract method to calculate area for Circle @Override double calculateArea() { return Math.PI * radius * radius; } // Implementation of abstract method to calculate perimeter for Circle @Override double calculatePerimeter() { return 2 * Math.PI * radius; } } // Concrete subclass implementing the Shape abstraction class Rectangle extends Shape { double length; double width; Rectangle(double length, double width) { this.length = length; this.width = width; } // Implementation of abstract method to calculate area for Rectangle @Override double calculateArea() { return length * width; } // Implementation of abstract method to calculate perimeter for Rectangle @Override double calculatePerimeter() { return 2 * (length + width); } } public class Main { public static void main(String[] args) { // Creating instances of Circle and Rectangle Circle circle = new Circle(5); Rectangle rectangle = new Rectangle(4, 6); // Calling methods circle.display(); System.out.println("Area of circle: " + circle.calculateArea()); System.out.println("Perimeter of circle: " + circle.calculatePerimeter()); rectangle.display(); System.out.println("Area of rectangle: " + rectangle.calculateArea()); System.out.println("Perimeter of rectangle: " + rectangle.calculatePerimeter()); } }
"Shape" is an abstraction class with two abstract methods "calculateArea()" and "calculatePerimeter()".
"Circle" and "Rectangle" are concrete subclasses of "Shape", each providing its implementation for the abstract methods.
The "display()" method is a concrete method in the "Shape" class, which is inherited by its subclasses.
Abstraction classes in Java provide a way to define common behaviour and characteristics while allowing subclasses to provide specific implementations.
They are a key part of object-oriented design, promoting code reusability and maintainability.