Single Inheritance using Java



Write a program in Java to implement Single Inheritance


Program:


class
Rectangle


 {


  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
Sk10


 {


  public static void main(String args[])


   {


    Box obj1=new Box(5.5,6.5,75);


    double a1= obj1.CalArea();


    double v1= obj1.CalVol();


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


    System.out.println("Volume=
"+v1);


    }


 }


Compile:


D:\>javac
Sk10.java


Run:


D:\>java
Sk10


Output:


Area =
35.75




Volume =
268.125


Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C