Blog

Java program to demonstrate inheritance

Java program to demonstrate inheritance

Inheritance in java : Basically one class acquire the properties of another class.

The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

Java program to demonstrate inheritance

package Q10_Inheritance;

public class InheritanceDemo
{
    public static void main(String[] args)
    {
        Polygon pol =new Polygon();
        Rectangle rec = new Rectangle();
        Triangle tri = new Triangle();

        rec. setValues(10,10);
        System.out.println("Rec: "+rec.areaOfRectangel());

        tri.setValues(10, 10);
        System.out.println("Tri: "+ tri.areaOfTriangle());
    }
}

class Polygon
{
    protected int width ;
    protected int height;

    protected void setValues(int a, int b)
    {
        width = a;
        height = b;
    }
}

class Rectangle  extends Polygon
{
    int areaOfRectangel()
    {
        return (width*height);
    }
}

class Triangle extends Polygon
{
    double areaOfTriangle()
    {
        return (width*height)/2;
    }
}