Program to demonstrate use of hierarchical inheritance using interface

This is simple program for hierarchical inheritance using interface. Here an interface Area is created having data member pi and method compute(float, float). After that two classes are created Rectangle and Circle. In these classes we have implemented Area interface and override the compute(float, float) method as shown in program.


PROGRAM
interface Area {
 
 final static float pi=3.14f;
 float compute(float x,float y);
}

class Rectangle implements Area {
 
 public float compute(float x,float y) {
  
  return(x*y);
 }
}

class Circle implements Area {
 
 public float compute(float x,float y) {
  
  return(pi*x*y);
 }
}

public class InterfaceTest {
 
 public static void main (String[] args) {
  
  Rectangle rect = new Rectangle();
  Circle cir = new Circle();
  Area area;
  area = rect;
  System.out.println("Area of Recatngle = "+area.compute(10,20));
  area = cir;
  System.out.println("Area of Circle = "+area.compute(10,10));
 }
}
OUTPUT
C:\>javac InterfaceTest.java
C:\>java InterfaceTest
Area of Recatngle = 200.0
Area of Circle = 314.0

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative