Inheritance Example 2 - Figure - Java

Program:
class figure
{
    int b;
    int l;
    figure(int x,int y)
    {
        l=x;
        b=y;
    }
}
class triangle extends figure
{
    triangle (int x,int y)
    {
        super(x,y);
    }
    int show()
    {
        return(l*b/2);
    }
}
class rectangle extends figure
{
    rectangle(int x,int y)
    {
        super(x,y);
    }
    int show()
    {
        return(l*b);
    }
}

class inheritance2
{
public static void main(String argrs[])
{
    figure fig1=new figure(4,5);
    triangle tri1=new triangle(4,5);
    rectangle rect1= new rectangle(4,5);
    System.out.println("Area of triangle="+tri1.show());
    System.out.println("Area of rectangle="+rect1.show());
}
}

Output:
nn@linuxmint ~ $ javac inheritance2.java
nn@linuxmint ~ $ java inheritance2
Area of triangle=10
Area of rectangle=20
nn@linuxmint ~ $

1 comment:

Related Posts Plugin for WordPress, Blogger...