Categories
Uncategorized

Hierarchal Inheritance

Simple program for Hierarchal inheritance

import java.util.Scanner;


class rectangle

{

double length,breadth,radius,heigth;

Scanner a=new Scanner(System.in);

void get()

{

System.out.println(“enter length and breadth:”);

length=a.nextDouble();

breadth=a.nextDouble();

}

void display()

{

double ar;

ar=length*breadth;

System.out.println(“Area of Rectangle:”+ar);

}

}

class circle extends rectangle

{

void get()

{

System.out.println(” Finding Area of circle and Rectangle:”);

super.get();

System.out.println(“Enter the Radius:”);

radius=a.nextDouble();

}

void display()

{

super.display();

double ar;

ar=3.14*radius*radius;

System.out.println(“Area of circle:”+ar);

}

}

class square extends rectangle

{

void get()

{

System.out.println(“Finding Area of square:”);

super.get();

}

void display()

{

double ar;

ar=length*length;

System.out.println(“Area of Square:”+ar);

}

}

class triangle extends rectangle

{

void get()

{

System.out.println(“Finding Area of Triangle:”);

super.get();

System.out.println(“Enter the height:”);

heigth=a.nextDouble();

}

void display()

{

double ar;

ar=0.5*breadth*heigth;

System.out.println(“Area of Triangle:”+ar);

}

}

class area2

{

public static void main(String args[])

{

triangle t=new triangle();

square s=new square();

circle c=new circle();

t.get();

c.get();

s.get();

s.display();

t.display();

c.display();

}

}

Note: There is unnecessary use of different variables in the program. so, please update if there is any new way of executing this program using Scanner class. please do comment below.