//WAP FOR METHOD OVERRIDING BY USING HIERARCHICAL INHERITANCE---
{
double dim1;
double dim2;
Figure(double a ,double b)
{
dim1=a;
dim2=b;
}
double area()
{
System.out.print("Area for figure is undefined..");
return 0;
}
}
class Rectangle extends Figure //inherit method and variable of figure class
{
Rectangle(double a , double b)
{
super(a,b); ///call super class constructor
}
double area()
{
System.out.print("Area for Rectangle is...");
return dim1*dim2;
}
}
class Square extends Figure //inherit method and variable of figure class
{
Square(double a , double b)
{
super(a,b); ///call super class constructor
}
double area()
{
System.out.print("Area for square is...");
return dim1*dim2;
}
}
class Triangle extends Figure //inherit method and variable of figure class
{
Triangle(double a , double b)
{
super(a,b); ///call super class constructor
}
double area()
{
System.out.print("Area for Triangle is...");
return dim1*dim2 / 2;
}
}
class Area
{
public static void main(String ar[])
{
double a;
Triangle t=new Triangle(10,8);
a=t.area();
System.out.println(a);
Rectangle r=new Rectangle(9,5);
a=r.area();
System.out.println(a);
Square s=new Square(5,5);
a=s.area();
System.out.println(a);
Figure f=new Figure(10,8);
a=f.area();
System.out.println(a);
}
}
PROGRAM CODE--
class Figure{
double dim1;
double dim2;
Figure(double a ,double b)
{
dim1=a;
dim2=b;
}
double area()
{
System.out.print("Area for figure is undefined..");
return 0;
}
}
class Rectangle extends Figure //inherit method and variable of figure class
{
Rectangle(double a , double b)
{
super(a,b); ///call super class constructor
}
double area()
{
System.out.print("Area for Rectangle is...");
return dim1*dim2;
}
}
class Square extends Figure //inherit method and variable of figure class
{
Square(double a , double b)
{
super(a,b); ///call super class constructor
}
double area()
{
System.out.print("Area for square is...");
return dim1*dim2;
}
}
class Triangle extends Figure //inherit method and variable of figure class
{
Triangle(double a , double b)
{
super(a,b); ///call super class constructor
}
double area()
{
System.out.print("Area for Triangle is...");
return dim1*dim2 / 2;
}
}
class Area
{
public static void main(String ar[])
{
double a;
Triangle t=new Triangle(10,8);
a=t.area();
System.out.println(a);
Rectangle r=new Rectangle(9,5);
a=r.area();
System.out.println(a);
Square s=new Square(5,5);
a=s.area();
System.out.println(a);
Figure f=new Figure(10,8);
a=f.area();
System.out.println(a);
}
}





Comments
Post a Comment