<< Chapter < Page Chapter >> Page >

The output produced by the code in Listing 22 is shown in Figure 15 .

It should come as no surprise that this object of the class A was instantiated with a constructor parameter value of 10, and that it was thesecond object of the class named A that was constructed.

This is simply a matter of code in the constructor for class C instantiating an object of atop-level class, and is no different in concept from instantiating an object of the member class B, also shown in Listing 11 .

Inheritance and containment hierarchies are independent

The remaining code is designed to demonstrate that the containment hierarchy is completely independent ofthe inheritance hierarchy.

The class named C is a member of (is contained in) the class named B. That constitutes a part of the containment hierarchy.

The class named C also extends the class named X, which in turn extends the class named Object . That constitutes the inheritance hierarchy.

Overridden toString methods

I'm going to put the discussion of the method named cShow on hold and return to that discussion shortly.

The class named X inherits, and overrides the toString method, as shown in Listing 2 . When this version of the toString method is called, it returns the string " toString in Class X ".

The class named C, which extends the class named X, also overrides the toString method as shown in Listing 23 .

When this version of the toString method is called, it returns the string

" toString in Class C ".

We will see the impact of overriding these two methods later.

Illustrate the inheritance hierarchy

Returning to the cShow method, the code in Listing 24 illustrates the inheritance hierarchy to which the class named C belongs by getting anddisplaying the value stored in the instance variables named className belonging to the object instantiated from the class named C.

(The object contains two instance variables having the name className . One of these instance variables was contributed to the object by thesuperclass named X. The other was contributed to the object by the class named C.)

Two instance variables named className

The String value X was stored in one of the instance variables named className by the initialization of the variable shown in Listing 2 .

The String value C was stored in the other instance variable named className by the initialization of the variable shown in Listing 7 .

( Note that the variable named className is protected in the class named X. A subclass method cannot access a private variable in a superclass. To be accessible by a subclass method, the superclass variablemust be protected , package-private , or public .)

Two overridden toString methods

An object instantiated from the class named C also contains two overridden versions of the toString method. One version of the method was contributed to the object by the superclass named X.The other version was contributed to the object by the class named C.

Call the toString methods

The code in Listing 24 also calls the two overridden toString methods belonging to the object instantiated from the classes named C. As explained earlier, one version of the toString method is overridden in the class named X and the other version is overridden in the class named C.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask