Creating a Multilevel Hierarchy : Inheritance « Class Definition « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Class Definition » Inheritance 
5.22.15.Creating a Multilevel HierarchyPrevious/Next
class Box {
  private double width;

  private double height;

  private double depth;

  Box(Box ob) { // pass object to constructor
    width = ob.width;
    height = ob.height;
    depth = ob.depth;
  }

  Box(double w, double h, double d) {
    width = w;
    height = h;
    depth = d;
  }

  Box() {
    width = -1// use -1 to indicate
    height = -1// an uninitialized
    depth = -1// box
  }

  Box(double len) {
    width = height = depth = len;
  }

  double volume() {
    return width * height * depth;
  }
}

class BoxWeight extends Box {
  double weight; // weight of box

  BoxWeight(BoxWeight ob) { // pass object to constructor
    super(ob);
    weight = ob.weight;
  }

  BoxWeight(double w, double h, double d, double m) {
    super(w, h, d)// call superclass constructor
    weight = m;
  }

  BoxWeight() {
    super();
    weight = -1;
  }

  BoxWeight(double len, double m) {
    super(len);
    weight = m;
  }
}

class Shipment extends BoxWeight {
  double cost;

  Shipment(Shipment ob) { // pass object to constructor
    super(ob);
    cost = ob.cost;
  }

  Shipment(double w, double h, double d, double m, double c) {
    super(w, h, d, m)// call superclass constructor
    cost = c;
  }

  Shipment() {
    super();
    cost = -1;
  }

  Shipment(double len, double m, double c) {
    super(len, m);
    cost = c;
  }
}

class DemoShipment {
  public static void main(String args[]) {
    Shipment shipment1 = new Shipment(102015103.41);
    Shipment shipment2 = new Shipment(2340.761.28);

    double vol;

    vol = shipment1.volume();
    System.out.println("Volume of shipment1 is " + vol);
    System.out.println("Weight of shipment1 is " + shipment1.weight);
    System.out.println("Shipping cost: $" + shipment1.cost);
    System.out.println();

    vol = shipment2.volume();
    System.out.println("Volume of shipment2 is " + vol);
    System.out.println("Weight of shipment2 is " + shipment2.weight);
    System.out.println("Shipping cost: $" + shipment2.cost);
  }
}
5.22.Inheritance
5.22.1.Inheritance
5.22.2.Accessibility
5.22.3.Method Overriding
5.22.4.The extends Keyword
5.22.5.Deriving a Class
5.22.6.The keyword super represents an instance of the direct superclass of the current object.
5.22.7.Derived Class Constructors: Calling the Base Class Constructor
5.22.8.Overriding a Base Class Method
5.22.9.Type Casting
5.22.10.Inheritance, constructors and arguments
5.22.11.Combining composition and inheritance
5.22.12.Inheritance and upcasting.
5.22.13.Overloading a base-class method name in a derived class does not hide the base-class versions.
5.22.14.Cleanup and inheritance
5.22.15.Creating a Multilevel Hierarchy
5.22.16.Demonstrate when constructors are called in a Multilevel Hierarchy
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.