Inheritance Hierarchies

Objectives
  1. Make an inheritance hierarchy by extending classes

  2. Implement constructor chaining

  3. Learn to distinguish between overloading and overriding

Getting Started
  1. Create a project called L8

  2. Create four classes called A, B, C, and Main

Your directory structure will look like this:

W4L2/
└── src
    ├── A.java
    ├── B.java
    ├── C.java
    └── Main.java
Description

This lab introduces you to the concepts of inheritance in Java. The basic idea is to build an inheritance hierarchy from scratch so that you can observe some of the behavior of super and subclasses. The example is completely artificial, but don’t worry, we’ll show you some good examples of the benefits of inheritance later. For the moment we want you to see the basic inheritance mechanisms of Java. Java is very similar to, but not exactly the same as, other OO (object oriented) languages such as C++ when it comes to inheritance.

The inheritance is as follows:

  • The A class will be the super class for B and C

  • B will be a subclass of A and a superclass of C

  • C will be a subclass of B, which is in turn a subclass of A

Tip
Another name for a subclass is a derived class or child class.

The picture below shows the inheritance hierarchy that you are building, using UML notation:

UML Diagram
Part One - Implementing Class A

Declare two protected fields of type double, named x and y.

Complete the rest of the class using the following javadoc.

Now you’ve implemented the A class, open the Main class, create a main method and add the following code:

// Instantiate and test A
A a = new A(2.0, 4.0);
System.out.printf("x: %.1f, y: %.1f\n", a.getX(), a.getY());
System.out.printf("a: %s\n", a);
System.out.println(a.toString("A class: "));
System.out.printf("a.sum: %.1f\n", a.sum());
System.out.printf("a.product: %.1f\n", a.product());
// System.out.println("a.power: " + a.power());
System.out.println("\n");
Part Two - Implementing Class B and Learning about Inheritance

Make class B a subclass of class A by adding "extends A" to the class declaration. This is an inheritance relationship, B now immediately inherits all public, package private, and protected members/methods in class A.

Complete the rest of the code using the javadoc

Note
there will be a compilation error. The call to the super constructor must come before any other code. If you’re interested, here’s some super reading (hahaha) information.

Append the following code at the end of the main method in the Main class
Run the code and answer the following questions:

// Instantiate and test B
B b = new B(3.0, 5.0);
System.out.printf("b: %s\n", b);
System.out.println(b.toString("B class: "));
System.out.printf("b.sum: %.1f\n", b.sum());
System.out.printf("b.product: %.1f\n", b.product());
System.out.printf("b.power: %.1f\n", b.power());
System.out.println("\n");

/* Why does the super class constructor have to come before any other code?
 *
*/

/* x and y were declared in class A. Why can you use x and y for the power method in class B?
 *
*/

/* What changes in the code would cause B to not have the member variables x and y?
 *
*/

/* What happens when you uncomment the call to a.power?
 *
 *
 * Why can't you call the power method using an instance of class A?
 *
*/
Part Three - Implementing Class C and Taking Inheritance Even Further
  1. Go to class C

  2. Make class C extend class B by adding "extends B" to the class declaration. C now inherits all the members and methods of class B and class A.

  3. Add a field of type double named z.

  4. Use the javadoc to complete the rest of the class

Append the following code at the end of the main method in the Main class:

// Instantiate and test C
C c = new C(2.0, 3.0, 4.0);
System.out.println("c: " + c);
System.out.println(c.toString("C class: "));
System.out.println(c.toString());
System.out.println("c.sum: " + c.sum());
System.out.println("c.product: " + c.product());
System.out.println("c.power: " + c.power());
System.out.println("\n");

In the main method of the C class, try calling the super class method for the sum, product, and power methods. This demonstrates the basic principle of inheritance - let the base class do whatever work it can, the subclass extends the base class to add new functionality.

Note
We stayed almost completely away from polymorphism and dynamic binding, which will be in a future lab.

Add and answer these last questions in the main method of the Main class.

/* When the code calls the overloaded toString(String) on the C instance,
 * in which classes does it run code? HINT: use the debugger with "step into"
 * to observe what classes are called. Be specific.
 *
 * When the code calls the  original toString() on the C instance,
 * in which classes does it run code?
 *
 * How does Java determine which version of a method to use in an inheritance hierarchy?
 *
 */
Submission

Please show your code and comments to the teaching assistant or helper to get credit for this lab.