Java Inheritance Programs
01-08-2017    2101 times
Inheritance

In This post, i describe some inheritance related program. Inheritance is also known as parent-child relationship or IS-A relationship. Protected member of the super class acts as a public member in the child class. All super class constructor is call before executing the child class constructor and super keyword is used to access the super class version of methods and veriables.

Tutorial at home
Tutorial at home

Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the super class of the hierarchy. The instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects of your classes and outputs each object's area (except Quadrilateral).

Diagram Of The Program
Diagram Of The Program

public class Quadrilateral
{
protected int x1,x2,x3,x4,y1,y2,y3,y4;
protected void setCoordinate(int a,int b,int c,int d,int e,int f,int g,int h)
{
x1=a;
y1=b;
x2=c;
y2=d;
x3=e;
y3=f;
x4=g;
y4=h;
}
}

public class Square extends Quadrilateral
{
Square(int a,int b,int c,int d,int e,int f,int g,int h)
{
setCoordinate(a,b,c,d,e,f,g,h);
}
int area()
{
int d=(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return d*d;
}
}

public class Rectangle extends Quadrilateral
{
Rectangle(int a,int b,int c,int d,int e,int f,int g,int h)
{
setCoordinate(a,b,c,d,e,f,g,h);
}
int area()
{
int d1=(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
int d2=(int)Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4));
return d1*d2;
}
}

Tutorial at home
Tutorial at home

public class Trapezoid extends Quadrilateral
{
private int height;
Trapezoid(int a,int b,int c,int d,int e,int f,int g,int h,int height)
{
setCoordinate(a,b,c,d,e,f,g,h);
this.height=height;
}
int area()
{
int d1=(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
int d2=(int)Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
return (int)((d1+d2)*height)/2;
}
}

public class Parallelogram extends Quadrilateral
{
private int height;
Parallelogram(int a,int b,int c,int d,int e,int f,int g,int h,int height)
{
setCoordinate(a,b,c,d,e,f,g,h);
this.height=height;
}
int area()
{
int d1=(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return d1*height;
}
}

Tutorial at home


public class TestQuadrilateral
{
public static void main(String []args)
{
Square sq=new Square(10,10,20,10,20,20,10,20);
System.out.println("Area Of The Square is " + sq.area());
Rectangle rec=new Rectangle(10,10,30,10,30,20,10,20);
System.out.println("Area Of The Rectangle is " + rec.area());
Parallelogram p=new Parallelogram(10,10,30,10,20,20,0,20,8);
System.out.println("Area Of The Parallelogram is " + p.area());
Trapezoid t=new Trapezoid(10,10,30,10,40,20,0,20,8);
System.out.println("Area Of The Trapezoid is " + t.area());
}
}

Tutorial at home

Output:

Area Of The Square is 100
Area Of The Rectangle is 200
Area Of The Parallelogram is 160
Area Of The Trapezoid is 240

softetechnologies
    Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
    Related Post
  Query About the post