Default Method in Abstract Class in Java


Write a Java program to demonstrate an abstract class having a default method

  • Shape is an abstract class with an abstract method draw() and a concrete method info(). The draw() method must be implemented by any concrete subclass, while the info() method provides a default implementation to print "This is a shape" to the console.
  • 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 calls its info() method to print information about the shape, which in this case is a circle.

Source Code

abstract class Shape
{
	abstract void draw();
 
	void info()
	{
		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)
	{
		Circle circle = new Circle();
		circle.draw();
		circle.info();
	}
}

Output

Drawing circle
This is a shape