by
PRO
FE S
S OR

SECTION X - INHERITANCE
Step 39: Single Root Inheritance
INHERITANCE is one of the main pillars of any Object Oriented Language.
Click on INHERITANCE for a general explanation of what it is.
We will be looking into INHERITANCE in terms of classes. A class can inherit the attributes (variables) and behavior (methods) of
another class (the parent class). Also, a class can have another class (a child class) that inherit its attributes and behavior.
A class that inherits another class is called:
A class that in inherited by other class is called:
An ideal situation is when class inheritance follows the "is a" relationship; that is, the child class "is a" parent class.
Let's say that we have a Vehicle class as the parent class, and the Truck class as the child class; then we can say that
the Truck "is a" Vehicle. (this case follows the "is a" relationship.)
Single Root Inheritance
JAVA supports single root inheritance. That means that a class can only inherit of only one other class.
In JAVA the top level class is the Object class (which is in the java.lang package),
and all classes are derived automatically at various levels from this Object class.
Multiple Inheritance is the case of a class inheriting from more than one class. This is not supported by JAVA.
Keyword 'extends'
To implement Inheritance, the keyword 'extends' is used in the class definition after its name and
followed by the parent class name. Since the Object class is the Root of all classes in JAVA, it is not necessary to use
the word 'extends' to derive from the Object class. The Object class is like the Mother of all classes.
Below see a Hierarchy Graphics Representation of three classes illustrating the Inheritance Concept. (One super-class and two derived classes)

Notice in above representation that Truck and Automobile classes are siblings, as they both inherit attributes and behavior of the Vehicle class.
The attributes and behavior are implemented by the members of the class. (variables and methods.)
The following represents the JAVA code for above hierachy:
{
void vehicleMethod() // Instance Method representing the class Behavior
{
class Truck extends Vehicle
{
void truckMethod() // Instance Method unique to this class
{
class Automobile extends Vehicle
{
void autoMethod() // Instance Method unique to this class
{
Code Re-usability is achieved, as there is no need to code these members in the derived classes.
It is important to notice that INHERITANCE is unidirectioinal.
Therefore, the super-class (Vehicle class) cannot invoke or access the attributes and methods of its sub-classes.
It is also convenient to know that Inheritance goes beyond one level of hierarchy. That means that a child class, can have a
parent class as well as a grandParent class and can even derived from classes of higher levels of hierarchy. (as many levels as desired).
For instance, when we look into GUI components' classes (in the 2nd MiniCourse) we can see that there are many
GUI classes that are located many levels down in their hierarchy.
by
PRO
FE S
S OR

SECTION X - INHERITANCE
Step 40: Members (Variables and Methods) Inherited
Members of a class
All members (Variables and Methods) of the super-class are passed down to the sub-classe. They do not need to be re-defined
in the derived classes.
The following hierachy Example comprises a Student class as the parent class, and 2 derived classes: Senior, and Freshmen classes.
The Senior, and Freshmen classes can access all the members of the Student class, the super-class, plus their own members.
Each child class has its own members which are unique and can only be accessed by its own class.
Example:
{
void doHomework()
{
class Senior extends Student
{
void work() // unique method of the Senior class
{
class Freshmen extends Student
{
void readBook() // unique method of the Freshmen class
{
class Date
{
class HighSchool
{
{
Freshmen debbie = new Freshmen(); // A Freshmen object "debbie" is created.
mike.graduationYear = 2002; // Instance Variable from Student class
mike.doHomework(); // method doHomework() from Student class is invoked.
// Members in 2 lines of code above are of Student class inherited by a Senior Object..
mike.promDate.day = 5; // a Senior Object promDate is assigned.
mike.promDate.month = 12;
mike.promDate.year = 2002;
mike.work(); // method work() from the Senior class is invoked.
// Members in 4 lines of code above are of Senior class.
debbie.graduationYear = 2002; // Instance Variable from Student class
debbie.doHomework(); // method doHomework() from Student class is invoked.
// Members in 2 lines of code above are of Student class inherited by a Freshmen Object..
debbie.initiationDate.day = 12; // A Freshmen Object initiationDate is assigned.
debbie.initiationDate.month = 2;
debbie.initiationDate.year = 2001;
debbie.readBook(); // method readBook() from the Freshmen class is invoked.
// Members in 4 lines of code above are of the Freshmen class.
by
PRO
FE S
S OR

SECTION X - INHERITANCE
Step 41: Overriding (Re-defining members)
Overriding
Overriding occurs when members are re-defined in the derived classes. The re-defined variables and methods are then
accessed and implemented by the derived class.
The variables would have the same names in both the super-class as well as in the sub-class.
The methods would have the same name and signature in both the super-class as well as in the sub-class.
If methods have the same name and signature but different return type, a compiler error would occur for a return type incompatibility.
If methods have the same name but different signature (no mattter what the return type is) then it's considered
Overloading (discussed in an earlier Section),
not Overriding, and that implies that the methods (from the super or sub class) can be used distinctively as each method is called with unique arguments.
Overriding would not take advantage of the re-usability of Inheritance, and therefore should only be limited to some attributes and
behavior, if necessary. If all the variables and methods of a sub-class are overriding the parent class,
there is no purpose in implementing Inheritance, unless there is a grandParent (or higher in the hiearachy) class which contains attributes and
behavior that are inherited.
An Example of Overriding follows below. In this example, we have a super-class the BankAccount, and two child classes: CheckingAccount and
SavingsAccount. We use the Transaction Class for instantiating these classes.
The CheckingAccount class will override a method and a variable. The SavingsAccount class will override the method.
{
double balance;
double calculateInterest(double amount)
{
// code to calculate Interest goes here
return newAmount;
class CheckingAccount extends BankAccount
{
double calculateInterest(double amount) // this method overrides the one in the super-class.
{
// code to calculate Interest goes here
return newAmount;
class SavingsAccount extends BankAccount
{
{
// code to calculate Interest goes here
return newAmount;
class Transaction
{
{
double amountSavings = 1167.50;
CheckingAccount clientChecking;
clientChecking = new CheckingAccount(); // an Object 'clientChecking' is created.
SavingsAccount clientSavings;
clientSavings = new SavingsAccount(); // an Object 'clientSavings' is created.
// ---------------------------------------------------------------------------
// Using the clientChecking object below:
interestChecking = clientChecking.calculateInterest(amountChecking);
// By virtue of overriding, the method implemented above is from the
// CheckingAccount class, not from the BankAccount class.
clientChecking.balance = amountChecking + interestChecking;
// By virtue of overriding, the Instance variable 'balance' above is from the CheckingAccount class,
// not from the BankAccount class.
// ---------------------------------------------------------------------------
// Using the clientSavings object below:
interestSavings = clientSavings.calculateInterest(amountSavings);
// By virtue of overriding, the method implemented above is from the
// SavingsAccount class, not from the BankAccount class.
clientSavings.balance = amountSavings + interestSavings;
// Above statement does not override the Instance variable 'balance', because 'balance' is defined
// in the BankAccount class, not in the Savings Account class.
by
PRO
FE S
S OR

SECTION X - INHERITANCE
Step 42: The 'final' Modifier
The 'final' Modifier
We already have discussed the user of the 'final' modifier to create or define constants.
There are other two uses of the 'final' modifier:
When declaring a class
When 'final' is used to declare a class, it will make that class sterile, that is, that class will not be able to have
children or sub-classes. No other class can inherit from that class.
{
When declaring a method
When 'final' is used to declare a method, that method cannot be overridden, that is, no other method of sub-classes below
can redefine it.
The Example below shows a parent and a child class. A compiler error occurs as the method 'test' in the child class is
attempting to override its counterpart method in the parent class.
{
{
// class definition continues here...
class Quiz extends Exercise
{
{
by
PRO
FE S
S OR

SECTION X - INHERITANCE
Step 43: Constructor Invocation at different Hierarchy Levels
Constructor Invocation when Instantiating a Derived class
When a child class is instantiated, the default Constructor of its parent class is first invoked, and then its own
Constructor is invoked.
If the child class happens to have a grand-parent class, then when instantiated, the default Constructor of the grand-parent
class is first invoked, then the parent default Constructor, and finally its own Constructor is invoked.
That is, any derived class when instantiated, the default Constructor of the highest level in the hierarchy is invoked first, and then the invocation of the other default Constructors continues down the hierarchy.
This process is automatic, and it's done so that memory is allocated for the attributes that are inherited from all the
higher level classes.
Now, it is important to notice that it is the default Constructor of the higher level class(es) that is invoked,
no matter if it is ar agument Constructor that is being used for instantiating the derived class.
Let's have an example of three classes at three different hierarchy levels: a Top class, a Middle class and a Bottom class. We will
use a Transaction class for instantiating these three classes.
{
{
Top(int tmpNumber)
{
class Middle
{
{
Middle(int tmpNumber)
{
class Bottom
{
{
Bottom(int tmpNumber)
{
class Transaction
{
{
It displays: "Top Default Constructor".
Middle middle = new Middle(); // The Top Default Constructor is invoked first,
// and then the Middle Default Constructor.
// It displays: "Top Default Constructor" followed by "Middle Default Constructor".
Bottom bottom = new Bottom(); // The Top Default Constructor is invoked first,
// and then the Middle Default Constructor, followed by the Bottom Constructor.
// It displays: "Top Default Constructor" , "Middle Default Constructor"
// and then "Bottom Default Constructor" in that order.
// Let's now instantiate a Bottom class using the Argument Constructor.
Bottom bottomNumber;
bottomNumber = new Bottom(8); // bottomNumber created and number initialized to 8.
// Here the Top Default Constructor is invoked first, then the Middle Default Constructor
// followed by the Bottom Argument Constructor which is the one used for instantiation.
// It displays: "Top Default Constructor", "Middle Default Constructor"
// and then "Bottom Argument Constructor" in that order.
by
PRO
FE S
S OR

SECTION X - INHERITANCE
Step 44: The 'super' Reference
The 'super' Reference
The 'super' reference is similar to the 'this' reference (earlier discussed), but it's used only where Inheritance is implemented.
It refers to the "instance of the super-class".
The 'super' reference can only be used in a Constructor or in an Instance Method, similar to the case of the 'this' reference.
However, one difference from the 'this' reference is that 'super'
cannot be passed as argument, and it cannot be used as a return value either.
There are 2 main ways in which 'super' can be implemented:
For referencing a Variable or Method:
When 'super' is used to reference a member (variable or method) is followed by a dot, and then the member name.
It would nulify overriding for that member.
Let's say that there is a getName() method in the derived class,
and another getName() method in the super-class. Then for example, in the derived class we have
This also applies to variables. Let's say that there is a variables 'year' in the derived class, and
also 'year' in the super-class. Then
In a Constructor to call a super-class Constructor.
This way of using 'super' is similar to the way that 'this' is used.
The reference 'super' invokes a Constructor of the super-class.
As in the case of 'this' it has to be the first line of code within the Constructor.
Using 'super' this way is very useful when creating Objects that need to be initialized by a particular Constructor of its super-class.
As we already discussed in the previous Step, every time a child class is instantiated the Default Constructor of its super-class in invoked first.
By utilizing 'super' in the Constructor of the derived class, we can now select which super-class Constructor we want
to invoke. It does not have to be the super-class Default Constructor. If we select a super-class Constructor that is not
the Default, then when initializing the Object of the derived class
it would invoke the Constructor selected by the 'super'.
As in the case of 'this', 'super' is followed by a set of parentheses the same way as if a Constructor would be invoked.
The examples below illustrates its use.
Example:
{
{
Quiz test2 = new Quiz(5); // Derived class instantiated using its Argument Constructor.
Exercise() // A Default Constructor
{
// code continues here
Exercise(int tmpNumber) // a Constructor with one argument
{
// code continues here
class Quiz extends Exercise
{
Quiz() // A Default Constructor
{
System.out.println("Derived class Default Constructor");
// code continues here
Quiz(int tmpNumber) // a Constructor with one argument
{
noQuestions = tmpNumber;
System.out.println("Derived class Argument Constructor");
// code continues here
// Note: super() in this Constructor is redundant, as any derived class when instantiated
// would automatically invoke the Default Constructor of its super-class.
Then the Quiz(5) class is instantiated with its Argument Constructor, and when
the program is executed it will display "Super-class Default Constructor" followed by "Derived class Argument Constructor".