Abstract Class in Java

You have got an idea how you can use super class sub class relation to design your program. Sometimes, you will have a situation where the superclass will not be able to implement some method, it is completely left to sub class. And, like this way sometimes a superclass will only have some method declaration, and no method implementation. This is a way to tell the sub classes to implement these methods.

But how can we ensure this? Some wicked subclass may choose not to implement the method from super class. There is a special way to guard in this kind of situation. Declare those methods in super class (which you want the subclass to implement) with ‘abstract’ modifier.

Let us have a look into the below example :

package com.java;

// 'abstract' modifier is used for class
public abstract class ShapeBasics {

	// abstract method
	public abstract void area(); // no method body

	// concrete method
	public void giveIntro() {
System.out.println("I am an abstract class and have an abstract method 'area'. Any class extending me " +
		"have to implement this 'area' method");

Crate the subclass as below :

package com.java;

public class TriangleShape extends ShapeBasics {

	double side1 = 10;
	double side2 = 15;
	// implementing 'area' method from ShapeBasics class
	public void area() {
		double area = side1 * side2;
		System.out.println("The area of triangle is :" + area);

	public static void main(String[] args) {
		TriangleShape triangleShape = new TriangleShape();


Output :

The area of triangle is :150.0

Check the first class very carefully. We have a method ‘area’ with declaration:

public abstract void area();

This method declaration is new to us. This method is declared with an abstract keyword. Why? Because this method does not have any implementation, look this method does not have a method body. No curly braces. Here ‘area’ is called an abstract method.

Now check the class definition:

public abstract class ShapeBasics

If a class does have one or more abstract method then we have to make the class an abstract class. That is the rule.

Look, abstract class can contain concrete method(s) also, a method with method body.

Like the ShapeBasics class containing a concrete method ‘giveIntro’. This is completely within rule.

Come to the second class ‘TriangleShape’. It is basically extending the ShapeBasics class.

Now because we have declared the ‘area’ method in ShapeBasics class as abstract, the TriangleShape class is bound to implement this method.

For checking purpose try to remove the ‘area’ method from TriangleShape class, the compiler will raise a compilation error saying the ‘area’ method must be implemented.

The rest of the TriangleShape class a nothing new. Run the TriangleShape class and you will get this output as expected:

The area of triangle is :150.0

Next > < Prev
Scroll to Top