Interface Basics in Java


Write a Java program to demonstrate the use of an interface

  • Shape is an interface that declares a method draw() without any implementation.
  • Circle is a class that implements the Shape interface. It provides an implementation for the draw() method specified in the interface.
  • In the Main class, an instance of Circle is created, and its draw() method is called. This method prints "Drawing a circle" to the console.

Source Code

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

Output

Drawing a circle

Example Programs