Final Method in Abstract Class in Java


Create a Java program to demonstrate an abstract class with a final method

  • Shape is an abstract class with an abstract method draw() and a final method display(). The draw() method must be implemented by any concrete subclass, while the display() method is implemented and cannot be overridden by subclasses.
  • Circle extends Shape and provides an implementation for the draw() method, printing "Drawing circle" to the console.
  • The Main class creates an instance of Circle, calls its draw() method to draw the circle, and then calls its display() method, which prints "Displaying shape" to the console. Since the display() method is final, it cannot be overridden by subclasses.

Source Code

abstract class Shape
{
	abstract void draw();
 
	final void display()
	{
		System.out.println("Displaying shape");
	}
}
 
class Circle extends Shape
{
	@Override
	void draw()
	{
		System.out.println("Drawing circle");
	}
}
 
public class Main
{
	public static void main(String[] args)
	{
		Circle circle = new Circle();
		circle.draw();
		circle.display();
	}
}

Output

Drawing circle
Displaying shape