Abstract class and keyword abstract

Abstract classes in Java are used to declare common characteristics of subclasses. An abstract class cannot be instantiated. It can only be used as a superclass for other classes that extend the abstract class. Abstract classes are declared with the abstract keyword. Abstract classes are used to provide a template or design for concrete subclasses down the inheritance tree.

Like any other class, an abstract class can contain fields that describe the characteristics and methods that describe the actions that a class can perform. An abstract class can include methods that contain no implementation. These are called abstract methods. An abstract class can contain no abstract methods also. The abstract method declaration must then end with a semicolon rather than a block. If a class has any abstract methods, whether declared or inherited, the entire class must be declared abstract. Abstract methods are used to provide a template for the classes that inherit the abstract methods.

Abstract classes cannot have objects; they must be subclassed, and actual implementations must be provided for the abstract methods. Any implementation specified can, of course, be overridden by additional subclasses. An object must have an implementation for all of its methods. We need to create a subclass that provides an implementation for the abstract method.

We can implement the generic Shape class as an abstract class so that we can draw shapes like rectangles, circles, squares etc. All these shapes have some common fields and methods, but each can, of course, add more fields and methods. The abstract class guarantees that each of these shapes will have the same set of basic properties. We declare this class abstract because there is no such thing as a generic shape. There can only be concrete shapes such as rectangles, circles, squares etc.

It is to be noted that an abstract class can also have constructors. Here is the code:

AbstractClassTest.java

abstract class Shape { //abstract class
double pi;

//constructor
Shape() {
pi = 3.14;
}

//concrete method
void show() {
System.out.println("PI = " + pi);
}

//abstract method
abstract double computeArea(double length, double breadth);
}

class Rectangle extends Shape {

double computeArea(double length, double breadth) {
return length * breadth;
}
}

class Circle extends Shape {

double computeArea(double length, double breadth) {
return pi * length * length;
}
}

class Square extends Shape {

double computeArea(double length, double breadth) {
return length * length;
}
}

public class AbstractClassTest {

public static void main(String[] args) {
Shape s; //reference
s = new Rectangle();
s.show();
System.out.println("AREA1 = " + s.computeArea(15, 10));
s = new Circle();
System.out.println("AREA2 = " + s.computeArea(10, 0));
s = new Square();
System.out.println("AREA3 = " + s.computeArea(20, 0));
}
}

Output:

PI = 3.14
AREA1 = 150.0
AREA2 = 314.0
AREA3 = 400.0

 

NOTE: Abstract classes can’t have objects and they are used to provide general definition for many concrete classes.