Abstraction Meaning in Java
What is abstraction?
Abstraction is a programming concept used in Java that enables you to build sophisticated systems by keeping implementation details hidden from the user and just exposing the functionality that is required. By developing abstract classes or interfaces that specify the overall behaviour of a group of related classes, it is a method for controlling complexity.
A class that cannot be created and has one or more abstract methods, methods without implementation is referred to as an abstract class. Subclasses of abstract classes are used to implement the abstract methods, and abstract classes are designed to be sub-classed. Subclasses might follow the abstract class as a guide to ensure that they have the same methods and properties.
An interface is a set of abstract methods and constants that establishes the terms under which a class must carry out its implementation. Similar to abstract classes, interfaces cannot be instantiated; instead, a class that implements the interface is required to implement the interface's functions. Without describing how those behaviours should be implemented, an interface defines a set of behaviours that a class must possess.
Because it encourages modularity, encapsulation, and information hiding, abstraction is a crucial notion in Java and other object-oriented programming languages. This makes programmes simpler to create, test, and maintain.
Abstract methods and their needs
In java, abstract method is referred as a declared method that is unimplemented in program. If we want to declare methods then we use the abstract keyword. These methods are intended to be overridden by a subclass.
Basic need of abstract methods is that we want to establish a class that provides a collection of behaviour or methods must be implemented by its subclasses. But we don’t want to provide the default implementation for those implementations. This leads to the necessity for abstract methods.
For example:
Think about a class hierarchy that represents various geometric forms, including circles, squares, and rectangles. Although the area and perimeter calculations for each form vary, all shapes share certain characteristics. In this example, we can create an abstract class named shape that will contain abstract methods for computing the area and the perimeter and after that it create a subclass for each distinct shape that will actually implement those methods.
Any class that derives from "Shape" must adhere to a contract set forth in the abstract methods of the "Shape" class. This guarantees that every concrete class that derives from "Shape" has an interface that is consistent and offers the functionality that is anticipated.
In conclusion, abstract methods are required when you want to create a class that specifies a set of behaviours that must be implemented by its subclasses but do not want to give a default implementation for those methods. You can specify a contract for all classes that inherit from an abstract class using abstract methods.
Code implementation
Let’s take a program to understand abstraction in details:
// Abstract class Shape abstract class Shape { // Abstract methods abstract double getArea(); abstract double getPerimeter(); } // Concrete class Circle that inherits from Shape class Circle extends Shape { // Properties private double radius; // Constructor public Circle(double radius) { this.radius = radius; } // Implementing the abstract methods of Shape @Override double getArea() { return Math.PI * radius * radius; } @Override double getPerimeter() { return 2 * Math.PI * radius; } } // Concrete class Rectangle that inherits from Shape class Rectangle extends Shape { // Properties private double width; private double height; // Constructor public Rectangle(double width, double height) { this.width = width; this.height = height; } // Implementing the abstract methods of Shape @Override double getArea() { return width * height; } @Override double getPerimeter() { return 2 * (width + height); } } // Main class to test the Shape hierarchy public class Main { public static void main(String[] args) { Shape shape1 = new Circle(5.0); Shape shape2 = new Rectangle(3.0, 4.0); // Calling the abstract methods of Shape through the concrete classes System.out.println("Area of shape1: " + shape1.getArea()); System.out.println("Perimeter of shape1: " + shape1.getPerimeter()); System.out.println("Area of shape2: " + shape2.getArea()); System.out.println("Perimeter of shape2: " + shape2.getPerimeter()); } }
Output:
Area of shape1: 78.53981633974483 Perimeter of shape1: 31.41592653589793 Area of shape2: 12.0 Perimeter of shape2: 14.0