Sponsered Links
Categories
Sponsered Links

Java inheritance example

 

This example shows java inheritance.

import java.io.*;
class Box 
{

    int width;
    int height;
    int depth;
    Box(int w, int h, int d) 
     {

      width = w;
      height =h;
      depth = d;
     }
  void getVolume()
 {

    System.out.println("Volume is : " + width * height * depth);
  }
}
class MatchBox extends Box 
{

    int weight;
    MatchBox(int w, int h, int d, int m) {
    super(w, h, d);
    weight = m;
  }
}
class inheritanceDemo
{  
  public static void main(String args[]) throws IOException
    {
      int width ,w;
      int height ,h;
      int depth,d ;
      int weight,m;
BufferedReader in=
new BufferedReader
     (
new InputStreamReader(System.in));
      System.out.print("enter the value of width   =    ");
      w= Integer.parseInt(in.readLine());
      System.out.print("enter the value of height  =    ");
      h= Integer.parseInt(in.readLine());
      System.out.print("enter the value of  depth  =    ");
      d= Integer.parseInt(in.readLine());
      System.out.print("enter the value of  weight =    ");
      m= Integer.parseInt(in.readLine());
      MatchBox mb1 = new MatchBox(w,h,d,m);
      mb1.getVolume();
      System.out.println("width of MatchBox one is =  " + mb1.width);
      System.out.println("height of MatchBox one is=  " + mb1.height);
      System.out.println("depth of MatchBox one is =  "+ mb1.depth);
      System.out.println("weight of MatchBox one is=  " + mb1.weight);
  }
}

 
 
Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved