The perfect place for easy learning...

Java Programming

×

Topics List


Java Abstract Class





An abstract class is a class that created using abstract keyword. In other words, a class prefixed with abstract keyword is known as an abstract class.

In java, an abstract class may contain abstract methods (methods without implementation) and also non-abstract methods (methods with implementation).

We use the following syntax to create an abstract class.

Syntax
abstract class <ClassName>{
    ...
}

Let's look at the following example java code.

Example
import java.util.*;

abstract class Shape {
	int length, breadth, radius;
	Scanner input = new Scanner(System.in);

	abstract void printArea();

}

class Rectangle extends Shape {
	void printArea() {
		System.out.println("*** Finding the Area of Rectangle ***");
		System.out.print("Enter length and breadth: ");
		length = input.nextInt();
		breadth = input.nextInt();
		System.out.println("The area of Rectangle is: " + length * breadth);
	}
}

class Triangle extends Shape {
	void printArea() {
		System.out.println("\n*** Finding the Area of Triangle ***");
		System.out.print("Enter Base And Height: ");
		length = input.nextInt();
		breadth = input.nextInt();
		System.out.println("The area of Triangle is: " + (length * breadth) / 2);
	}
}

class Cricle extends Shape {
	void printArea() {
		System.out.println("\n*** Finding the Area of Cricle ***");
		System.out.print("Enter Radius: ");
		radius = input.nextInt();
		System.out.println("The area of Cricle is: " + 3.14f * radius * radius);
	}
}

public class AbstractClassExample {
	public static void main(String[] args) {
		Rectangle rec = new Rectangle();
		rec.printArea();

		Triangle tri = new Triangle();
		tri.printArea();
		
		Cricle cri = new Cricle();
		cri.printArea();
	}
}

When we run this code, it produce the following output.

abstract class in java

🔔 An abstract class can not be instantiated but can be referenced. That means we can not create an object of an abstract class, but base reference can be created.

In the above example program, the child class objects are created to invoke the overridden abstract method. But we may also create base class reference and assign it with child class instance to invoke the same. The main method of the above program can be written as follows that produce the same output.

Example
public static void main(String[] args) {
		Shape obj = new Rectangle();  //Base class reference to Child class instance
		obj.printArea();

		obj = new Triangle();
		obj.printArea();
		
		obj = new Cricle();
		obj.printArea();
	}

8 Rules for method overriding

An abstract class must follow the below list of rules.

  • An abstract class must be created with abstract keyword.
  • An abstract class can be created without any abstract method.
  • An abstract class may contain abstract methods and non-abstract methods.
  • An abstract class may contain final methods that can not be overridden.
  • An abstract class may contain static methods, but the abstract method can not be static.
  • An abstract class may have a constructor that gets executed when the child class object created.
  • An abstract method must be overridden by the child class, otherwise, it must be defined as an abstract class.
  • An abstract class can not be instantiated but can be referenced.