Abstract Class with Static Method in Java


Write a Java program to demonstrate the use of an abstract class having a static method

  • The Shape class is declared as abstract with an abstract method draw() and a static method display().
  • The Circle class extends the Shape class and provides its own implementation for the draw() method.
  • In the main method: We directly call the static method display() of the Shape class without needing to create an object of Shape. Static methods belong to the class itself, not to instances of the class. create an object of Circle class and call its draw() method, which prints "Drawing Circle".
  • Since the draw() method is abstract in the Shape class, it must be overridden in the subclass Circle. However, the display() method is static and not abstract, so it doesn't need to be overridden.

Source Code

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

Output

This is a Shape
Drawing Circle