CSC 205 Lab 2: Inheritance Hierarchy

Goals

After doing this lab, you should be able to:

  • Understand downcasting and upcasting
  • Override methods inherited from a superclass
  • Chaining constructors using this() and super()

Lab Setup

The source code files we are going to use as a code base are located in the following directory.

Courses on ‘blackhawk’\Martin Zhao\205\labs\lab2

Make a folder for you on the C: drive temporarily or on a floppy disk. Make a directory (desirably csc205/labs/lab2) on Cobra also and ftp your code there after you’ve finished.

  1. Inheritance: The Basic Concepts

In the following diagram, use proper notation to represent the inheritance relationship between classesjava.lang.Object and java.lang.String.

/ 1.1.Fill in Object and String in the right places.
1.2.Use appropriate link which represents inheritance relationship to connect the two class diagrams.
1.3.Fill in the following terms along the right arrows in the diagrams:
Generalization
Specialization
  1. Inheriting fromjava.lang.Object

Compile and execute the following code listing and answer questions.

class Client {

public static void main(String args[]) {

String stringRef = new String("Java");// (1)

System.out.println("(2): " + stringRef.getClass());//(2)

System.out.println("(3): "+ stringRef.length());//(3)

Object objRef = stringRef;//(4)

//System.out.println("(5):+ objRef.length());//(5) Not OK

System.out.println("(6): "+ objRef.equals("Java"));//(6)

System.out.println("(7): " + objRef.getClass());//(7)

stringRef = (String)objRef;// (8)

System.out.println("(9): " + stringRef.equals("C++"));// (9)

}

}

2.1. In the follow table, list methods called in the program under the class in which they are defined. Use Appendix C in King’s book as a reference or use Java online resource at Mark the method(s) which is (are) overridden by String.

java.lang.Object / java.lang.String

2.2. Compare the ways the getClass() method is invoked in lines labeled (2) and (7), why both of them are legal? And why both method calls yield the same result?

2.3. Similarly, compare lines (3) and (5), why is it NOT OK to invoke the length() method from objRef, which is simply a substitute for stringRef?

2.4. Compare lines (4) and (8), what can you summarize for up- and downcasting?

  1. Method Overriding and Variable Shadowing

Subclasses may override non-static methods inherited from the superclass for certain variations which can help fulfilling their responsibilities in the right way. Read the code listings for the class Light and its subclass (in Client2.java) and answer the following questions.

Similarly, a subclass can change the value in a variable inherited from its superclass. This phenomenon is known as variable shadowing.

In both cases, a less restrictive access modifier may be used in the subclass for an overriding method or reassigned variable.

3.1. Find out an example of overriding and one for variable shadowing. And notice the different access modifier used in Light and TubeLight classes.

3.2. Is the method defined in line (5) an example of overriding? If not, what is it then? And why?

3.3. Compare the exceptions may be thrown in the overriding and overridden methods. Should the one in the overriding method be a narrower or wider type? And explain your answer.

3.4. Compile and execute Client2.java, and recorder the result.

  1. Object Reference this and super

Client.java provides an implementation of the NeonLight class, which extends the TubeLight class.

4.1. Read the code listings in Client.java. Will it compile without the definition of Light, TubeLight, and the exception classes? Why? Verify your answer by trying to compile it.

4.2. For each of the method calls in lines (1) through (3), describe the detailed invocation sequence up the inheritance hierarchy. You may execute the program first and then explain the result, or give your answers first and then verify them by executing the code.

4.3. In lines (4) and (5), what is it trying to doby using the expression (Light)this? Especially, does it make any difference if super were used instead?

4.4. After answering 4.3, think about the following questions?

  • Is it legal to use super.super when you want to refer to resources in Light from NeonLight?
  • What is the right way to do it if you really need to do so?
  1. Chaining Constructors Using this() and super()

5.1. Did it bother you that none of the light classes has a constructor defined? Have we really created objects in various Light types? What happened, say, when you tried to create a TubeLight object in line (7) of Client2.java?

5.2.To review how to write constructors using this(), a new version of the Light class is given in DemoThisCall.java. Compile and execute this program and record the output. Explain what happened when line (2) was executed.

5.3. At this point, try to execute Client3. Does it run? Why? How can you fix this?

5.4. Compile and execute Chaining.java and record the output. Explain what happened when it is executed.

Deliverables

A hard copy of answers to the questions asked in this lab. Due by Friday, Jan 24.

1