abstract Keyword in Java with Examples

The abstract keyword is used to declare a class or a method as abstract. An abstract class is a class that is declared abstract means it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation.
Basically, an abstract keyword is used to declares a class as an abstract class or abstract method. Let’s discuss what is an abstract class and abstract method with examples.

abstract关键字用于将类或方法声明为抽象。 抽象类是一个声明为抽象的类意味着它可能包含也可能不包含抽象方法。 抽象类无法实例化,但可以进行子类化。
抽象方法是在没有实现的情况下声明的方法。
基本上,abstract关键字用于将类声明为抽象类或抽象方法。 让我们用示例讨论什么是抽象类和抽象方法。

1. Abstract Class

An abstract class is a class that is declared abstract means it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

抽象类定义规则:

  • Abstract classes cannot be instantiated directly.
  • Abstract classes may not be marked as private or final.
  • An abstract class that extends another abstract class inherits all of its abstract methods as its own abstract methods.
    The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods.
    Read more about abstraction at Abstraction in Java with Example
    Read more about Abstract class at What is Abstract Class and Abstract Method in Java with Examples
    Animal hierarchy example
    In this example, let’s create an abstract Animals class with an abstract sound() method, the Dog and Cat classes extends Animals class and provide their own implementation to sound() method.
abstract class Animals{
    private String name;
 
    // All kind of animals eat food to make this common to all animals
    public void eat(){
         System.out.println(" Eating ..........");
    }
 
    // The animals make different sounds. They will provide their own implementation
    abstract void sound(); 
}

class Cat extends Animals{

    @Override
    void sound() {
        System.out.println("Meoww Meoww ........");
    }
}

class Dog extends Animals {

    @Override
    void sound() {
         System.out.println("Woof Woof ........");
    }
}

public class AbstractClassCompleteExample {

    public static void main(String[] args) {
         Animals animals = new Cat();
         animals.sound();
  
         animals = new Dog();
         animals.sound();
    }
}

Shapes Examples
Let’s create an abstract DrawObject class with draw() and resize() methods. Each non-abstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw() and resize() methods:

package com.javaguides.corejava.keywords.abstractkeyword;

public class AbstractKeywordShapesExample {
    public static void main(final String[] args) {
        final DrawObject circle = new Circle(10, 20);
        final DrawObject line = new Line(10, 20);
        final DrawObject reactangle = new Reactangle(10, 20);
        circle.draw();
        line.draw();
        reactangle.draw();
    }

}


abstract class DrawObject {
    int x, y;

    public DrawObject(final int x, final int y) {
        super();
        this.x = x;
        this.y = y;
    }

    void moveTo(final int newX, final int newY) {
        System.out.println(" move GraphicObjects points :: " + newX + " " + newY);
    }

    abstract void draw();

    abstract void resize();
}

class Rectangle extends DrawObject {

    public Rectangle(final int x, final int y) {
        super(x, y);
    }

    @Override
    void draw() {
        System.out.println("Draw Reactangle");
    }

    @Override
    void resize() {
        // TODO Auto-generated method stub
    }

}

class Line extends DrawObject {

    public Line(final int x, final int y) {
        super(x, y);
    }

    @Override
    void draw() {
        System.out.println("Draw Line");
    }

    @Override
    void resize() {

    }
}

class Circle extends DrawObject {

    public Circle(final int x, final int y) {
        super(x, y);
    }

    @Override
    void draw() {
        System.out.println("Draw Circle");
    }

    @Override
    void resize() {

    }
}

2. Abstract Method

An abstract method is a method that is declared without an implementation.
Abstract Method Definition Rules:
Abstract methods may only be defined in abstract classes.
Abstract methods may not be declared private or final.
Abstract methods must not provide a method body/implementation in the abstract class for which is it declared.
Implementing an abstract method in a subclass follows the same rules for overriding a method.
Example: The below AbstractShape class contains two abstract methods - draw() and moveTo();

abstract class AbstractShape{
    abstract void draw();
    abstract void moveTo(double deltaX, double deltaY);
}

Summary:

A class with an abstract method is inherently abstract and must be declared abstract.

An abstract class cannot be instantiated.

A subclass of an abstract class can only be instantiated if it implements all of the abstract methods of its superclass. Such classes are called concrete classes to differentiate them from abstract classes.

If a subclass of an abstract class does not implement all of the abstract methods of its superclass, the subclass is also abstract.

The abstract keyword cannot be applied to methods that are static, private or final since such methods cannot be overridden and therefore cannot be implemented in subclasses.

No methods of a final class may be abstract since a final class cannot be subclassed.