What is Abstract Keyword in java

Introduction 

An abstract keyword is declared without any implementation or body, meaning it has no code. Instead, it is designed to be overridden and implemented by a subclass in Java.

To declare an abstract method in Java, you use the abstract keyword in the method declaration.

Syntax

abstract returnType methodName(parameterType parameterName);

Here, "abstract" is the keyword that indicates that the method is abstract, "returnType" is the data type of the value returned by the method, "methodName" is the name of the method, "parameterType" is the data type of the parameter passed to the method, and "parameterName" is the name of the parameter.

For example, consider an abstract class called "Shape" with an abstract method called "getArea," which calculates the Shape's area. The declaration of this method would be.

public abstract double getArea();

This method would be implemented by any concrete subclasses of "Shape" that define specific shapes such as squares, circles, or triangles. By using abstract methods, the abstract class can define a common interface for all the subclasses while allowing each subclass to implement the method in its way.

Abstract Class in Java

A class must be declared abstract if it includes any abstract methods. The following are several prohibited combos of additional modifiers for abstract modifier methods. 

  • final
  • abstract native
  • abstract static 
  • abstract synchronized 
  • abstract private 
  • abstract strictfp

Print the addition and subtraction using the abstraction method to show the technique.

abstract class java_demo
{
    abstract void printInfo();
}

class add extends java_demo
{
    void printInfo ()
    {
        int a = 6;
        int b = 8;
        System.out.println(a+b);
    }
}

class sub extends java_demo
{
    void printInfo()
    {
        int c = 9;
        int d = 7;
        System.out.println(c-d);
    }
}

class abstraction
{
    public static void main(String args[])
    {
        java_demo n = new add();
        n.printInfo();
        
        java_demo y = new sub();
        y.printInfo();
    }
}

This program defines the java_demo abstract class, which contains an abstract printInfo() method implemented by its concrete subclasses add and sub. In the added class, the printInfo() method prints the sum of two integers, a and b. In the sub-class, the printInfo() method prints the difference between two integers, c, and d. In the abstraction class's main method, two objects are created, one of type add and the other of type sub. The printInfo() method is called on each object, which calls for implementing printInfo() in the respective subclass. The output of the program is.

Output

 

This output is generated by the calls to System.out.println() in the printInfo() methods of the add and subclasses.

Abstract keyword with classes and methods.  

The"abstract" keyword creates abstract classes and abstract methods. An abstract class is a class that cannot be instantiated directly but can be subclassed. Abstract methods are declared without a body and must be implemented by concrete subclasses in Java. 

Here's an example of an abstract class.

public abstract class Shape {
    public abstract double getArea();
    public abstract double getPerimeter();
}

In this example, the Shape class is declared abstract and contains two abstract methods: getArea() and getPerimeter(). Any subclass of Shape must implement these two methods.

Here's an example of a subclass of Rectangle.

public class Demo  {
    public static void main(String[] args) {
        Rectangle rectangle1 = new Rectangle(5.0, 3.0); // Create a rectangle object with width 5.0 and height 3.0
        Rectangle rectangle2 = new Rectangle(2.5, 7.0); // Create another rectangle object with width 2.5 and height 7.0

        // Print the areas and perimeters of the two rectangles
        System.out.println("Rectangle 1:");
        System.out.println("Area: " + rectangle1.getArea());
        System.out.println("Perimeter: " + rectangle1.getPerimeter());

        System.out.println("Rectangle 2:");
        System.out.println("Area: " + rectangle2.getArea());
        System.out.println("Perimeter: " + rectangle2.getPerimeter());
    }
}

class Rectangle {
    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);
    }
}

This program has a Demo class with a main method that creates two Rectangle objects and calls their getArea and getPerimeter methods to print out their areas and perimeters. The Rectangle class is defined at the bottom of the program and has two private fields, width and height, representing the Rectangle's dimensions.

The Rectangle class also has a constructor that takes in the width and height as arguments and initializes the fields with them. Finally, the Rectangle class has two methods, getArea, and getPerimeter, which return the area and perimeter of the Rectangle, respectively. When you run this program, you should see an output that looks like this.

Output  

 

Conclusion 

In this article, you will learn about the code that taught us what the abstract method is in Java

FAQs  

Q- What is an abstract method in Java?

A-  An abstract method is a method in a Java class that is declared but not implemented in the class. It is used to define a method signature that any concrete subclass of the abstract class must implement

Q- Can you instantiate an abstract class in Java?

A- No, you cannot instantiate an abstract class in Java. An abstract class is a class that is meant to be extended and is not meant to be instantiated on its own.

Q- What is the difference between an abstract class and an interface in Java?

A- An abstract class can have instance variables, constructors, and non-abstract methods, while an interface cannot. Additionally, a class can only extend one abstract class but can implement multiple interfaces.

Q- When should you use an abstract class in Java?

A- We should use an abstract class in Java when we want to define a common behavior for a group of related classes but do not want to explain it. Instead, you want to leave the implementation of the behavior to the subclasses that extend the abstract class.


Similar Articles