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.
interface Shape
{
void input();
void area();
}
class Circle implements Shape
{
int r = 0;
double pi = 3.14, ar = 0;
@Override
public void input()
{
r = 5;
}
@Override
public void area()
{
ar = pi * r * r;
System.out.println("Area of circle:"+ar);
}
}
class Rectangle extends Circle
{
int l = 0, b = 0;
double ar;
public void input()
{
super.input();
l = 6;
b = 4;
}
public void area()
{
super.area();
ar = l * b;
System.out.println("Area of rectangle:"+ar);
}
}
public class Demo
{
public static void main(String[] args)
{
Rectangle obj = new Rectangle();
obj.input();
obj.area();
}
}
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]Related Posts:
- Check Java Books
- Apply for Computer Science Internship
- Practice Programming MCQs
- Practice BCA MCQs
- Check Programming Books