Single Inhertance



Write a Program to
implement the concept of Single inheritance where Super Class and Subclass has
its own constructor and, all the variables in the super class are defined
protected.





Program:


class Rectangle


 {


  protected double l,b;





  Rectangle(double x,double y)


   {


    l=x;


    b=y;


   }


  double CalArea()


    {


     return (l*b);


    }


 }


class Box extends
Rectangle


 {


  double h;


  Box(double x,double y,double z)


   {


    super(x,y);


    h=z;


   }


  double CalVol()


    {


     return (l*b*h);


    }


 }


class Sk12


 {


 


  public static void main(String args[])


   {


    Box obj=new Box(5.5,6.5,7.5);


    double a= obj.CalArea();


    double v= obj.CalVol();


    System.out.println("Area= "+a);


    System.out.println("Vol= "+v);


    }


 }


Compile:


D:\>javac Sk12.java


Run:


D:\>java Sk12


Output:


Area= 35.75




Vol= 268.175


0 Comments