Another Inheritance Example: Person - Student


A Simple Inheritance Example

The simple inheritance example program presented in this section expands on the UML diagram shown in figure 11-2. The behavior implemented by BaseClass is kept intentionally simple so you can concentrate on the topic of inheritance. You’ll be introduced to more complex programs soon enough.

The UML Diagram

A more complete UML diagram showing the fields and methods of BaseClass and DerivedClass is presented in figure 11-3

image from book
Figure 11-3: UML Diagram of BaseClass & DerivedClass Showing Fields and Methods

Referring to figure 11-3 — BaseClass contains one private field named _message which is of type String. BaseClass has four public methods: two constructors, printMessage(), and setMessage(). One of the constructors is a default constructor. A default constructor is simply a constructor that takes no arguments. The second constructor has one parameter of type String named message. Based on this information, objects of type BaseClass can be created in two ways. Once an object of type BaseClass is created the printMessage() or the setMessage() methods can be called on that object.

DerivedClass has its own private _message field and two constructors that are similar to the constructors found in BaseClass. Let’s now take a look at the source code for each class.

BaseClass Source Code

Referring to example 11.1 — BaseClass is fairly simple. Its first constructor begins on line 4 and declares one String parameter named message. The _message field is set by concatenating the message parameter and the String literal Message from BaseClass:”. The default constructor begins on line 8. It calls the first constructor with the String literal BaseClass message!”. The printMessage() method begins on line 12. It simply prints the _message field to the console. The setMessage() method begins on line 16. Its job is to change the value of the _message field.

Example 11.1: BaseClass.java

image from book
 1    public class BaseClass { 2      private String _message = null; 3 4      public BaseClass(String message){ 5        _message = "Message from BaseClass: " + message; 6      } 7 8      public BaseClass(){ 9        this("BaseClass message!"); 10     } 11 12     public void printMessage(){ 13     System.out.println(_message); 14     } 15 16     public void setMessage(String message){ 17      _message = "Message from BaseClass: " + message; 18     } 19   } 
image from book

Since all methods have a body, and are therefore defined, the BaseClass is also a concrete class. This means that objects of type BaseClass can be created with the new operator.

DerivedClass Source Code

Referring to example 11.2 — DerivedClass inherits the functionality of BaseClass by extending BaseClass using the extends keyword on line 1. DerivedClass itself provides two constructors and a private field named _message. The first constructor begins on line 4. It declares a String parameter named message. The first thing this constructor does, however, on line 5, is call the String parameter version of the BaseClass constructor using the super() method with the message parameter as an argument. If the super() method is called in a derived class constructor it must be the first line of code in the constructor body as is done here. The next thing the DerivedClass constructor does is set the value of its _message field to the value of the message parameter. (Remember, these are references to String objects.)

Example 11.2: DerivedClass.java

image from book
 1    public class DerivedClass extends BaseClass { 2      private String _message = null; 3     4       public DerivedClass(String message){ 5         super(message); 6         _message = message; 7       } 8 9       public DerivedClass(){ 10        this("DerivedClass message!"); 11      } 12   }
image from book

DerivedClass’s default constructor begins on line 9. It calls its version of the String parameter constructor using the String literal “DerivedClass message!” as an argument, which, as you learned above, then calls the BaseClass constructor via the super() method.

Let’s now take a look at how these two classes can be used in a program.

DriverApplication Program

The DriverApplication class is used to test the functionality of BaseClass and DerivedClass. The important thing to note in this example is what type of object is being declared and created on lines 3 through 5. Starting on line 3 a BaseClass reference named bcr1 is declared and initialized to point to a BaseClass object. On line 4 another BaseClass reference named bcr2 is declared and initialized to point to a DerivedClass object. On line 5 a DerivedClass reference named dcr1 is declared and initialized to point to a DerivedClass object. Note that a reference to a base class object can point to a derived class object. Also note that for this example only the default constructors are being used to create each object. This will result in the default message text being assigned to each object’s _message field.

Continuing with example 11.3 — on lines 7 through 9 the printMessage() method is called on each reference. It’s time now to compile and run the code. Figure 11-4 gives the results of running example 11.3.

Example 11.3: DriverApplication.java

image from book
 1    public class DriverApplication { 2      public static void main(String[] args){ 3        BaseClass bcr1 = new BaseClass(); 4        BaseClass bcr2 = new DerivedClass(); 5        DerivedClass dcr1 = new DerivedClass(); 6 7        bcr1.printMessage(); 8        bcr2.printMessage(); 9        dcr1.printMessage(); 10     } 11   }
image from book

image from book
Figure 11-4: Results of Running Example 11.3

As you will notice from studying figure 11-4 there are three lines of program output that correspond to the three printMessage() method calls on each reference bcr1, bcr2, and dcr1. Creating a BaseClass reference and initializing it to a BaseClass object results in the BaseClass version (the only version at this point) of the printMessage() method being called which prints the default BaseClass text message.

Creating a BaseClass reference and initializing it to point to a DerivedClass object has slightly different behavior, namely, the value of the resulting text printed to the console reflects the fact that a DerivedClass object was created, which resulted in the BaseClass _message field being set to the DerivedClass default value. Note that DerivedClass does not have a printMessage() method therefore it is the BaseClass version of printMessage() that is called. This behavior is inherited by DerivedClass.

Finally, creating a DerivedClass reference and initializing it to point to a DerivedClass object appears to have the same effect as the previous BaseClass reference -> DerivedClass object combination. This is the case in this simple example because DerivedClass simply inherits BaseClass’s default behavior and, except for its own constructors, leaves it unchanged.

Quick Review

A base class implements default behavior in the form of methods that can be inherited by derived classes. There are three reference -> object combinations: 1) if the base class is a concrete class, meaning it is not abstract, then a base class reference can point to a base class object, 2) a base class reference can point to a derived class object, and 3) a derived class reference can point to a derived class object.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net