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
advertisement

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.