Java Program to Implement Shape Interface using Circle and Rectangle Class

This is a Java Program to Make Shape as an Interface and Implement it using Circle and Rectangle Class.

Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are
declared without any body. Here we make interface as Shape with two methods as input() and area() which are implemented by further two classes as circle and rectangle who implements the interface Shape.

Here is the source code of the Java Program to Make Shape as an Interface and Implement it using Circle and Rectangle Class. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. interface Shape
  2. {
  3.     void input();
  4.     void area();
  5. }
  6. class Circle implements Shape
  7. {
  8.     int r = 0;
  9.     double pi = 3.14, ar = 0;
  10.     @Override
  11.     public void input()
  12.     {
  13.         r = 5;
  14.     }
  15.     @Override
  16.     public void area()
  17.     {
  18.         ar = pi * r * r;
  19.         System.out.println("Area of circle:"+ar);
  20.     }
  21. }
  22. class Rectangle extends Circle
  23. {
  24.     int l = 0, b = 0;
  25.     double ar;
  26.     public void input()
  27.     {
  28.         super.input();
  29.         l = 6;
  30.         b = 4;
  31.     }
  32.     public void area()
  33.     {
  34.         super.area();
  35.         ar = l * b;
  36.         System.out.println("Area of rectangle:"+ar);
  37.     }
  38. }
  39. public class Demo
  40. {
  41.     public static void main(String[] args)
  42.     {
  43.         Rectangle obj = new Rectangle();
  44.         obj.input();
  45.         obj.area();
  46.     }
  47. }

Output:

$ javac Demo.java
$ java  Demo
 
Area of circle:78.5
Area of rectangle:24.0

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

👉 For weekly programming practice and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels
advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations