Académique Documents
Professionnel Documents
Culture Documents
Inheritance
1.1 Objectives
In this section, we will be discussing about the concept of inheritance and how it is
applied in Java. We will also take a look at the effects and certain behaviors of classes
when inheritance takes effect.
Any class above a specific class in the class hierarchy is known as a superclass. While
any class below a specific class in the class hierarchy is known as a subclass of that
class.
Introduction to Programming I 1
J.E.D.I.
/**
* Default constructor
*/
public Person(){
System.out.println(Inside Person:Constructor);
name = "";
address = "";
}
/**
* Constructor with 2 parameters
*/
public Person( String name, String address ){
this.name = name;
this.address = address;
}
/**
* Accessor methods
*/
public String getName(){
return name;
}
Notice that, the attributes name and address are declared as protected. The reason we
did this is that, we want these attributes to be accessible by the subclasses of the
superclass. If we declare this as private, the subclasses won't be able to use them. Take
note that all the properties of a superclass that are declared as public, protected and
default can be accessed by its subclasses.
Introduction to Programming I 2
J.E.D.I.
Now, we want to create another class named Student. Since a student is also a person,
we decide to just extend the class Person, so that we can inherit all the properties and
methods of the existing class Person. To do this, we write,
When a Student object is instantiated, the default constructor of its superclass is invoked
implicitly to do the necessary initializations. After that, the statements inside the
subclass are executed. To illustrate this, consider the following code,
In the code, we create an object of class Student. The output of the program is,
Inside Person:Constructor
Inside Student:Constructor
Introduction to Programming I 3
J.E.D.I.
subclass will result in the execution of relevant constructor from the superclass, based
on the arguments passed.
For example, given our previous example classes Person and Student, we show an
example of a super constructor call. Given the following code for Student,
public Student(){
super( "SomeName", "SomeAddress" );
System.out.println("Inside Student:Constructor");
}
This code calls the second constructor of its immediate superclass (which is Person) and
executes it. Another sample code shown below,
public Student(){
super();
System.out.println("Inside Student:Constructor");
}
This code calls the default constructor of its immediate superclass (which is
Person) and executes it.
There are a few things to remember when using the super constructor call:
Another use of super is to refer to members of the superclass (just like the this
reference ). For example,
public Student()
{
super.name = somename;
super.address = some address;
}
The parameter name in this declaration is age, which has the same name as the instance
variable age. Since the parameter age is the closest declaration to the method, the value
of the parameter age will be used. So in the statement,
age = age;
Introduction to Programming I 4
J.E.D.I.
we are just assigning the value of the parameter age to itself! This is not what we want
to happen in our code. In order to correct this mistake, we use the this reference. To
use the this reference, we type,
this.<nameOfTheInstanceVariable>
This method will then assign the value of the parameter age to the instance variable of
the object StudentRecord.
NOTE: You can only use the this reference for instance variables and NOT static
or class variables.
1: public StudentRecord(){
2: this("some string");
3:
4: }
5:
6: public StudentRecord(String temp){
7: this.name = temp;
8: }
9:
10: public static void main( String[] args )
11: {
12:
13: StudentRecord annaRecord = new StudentRecord();
14: }
Given the code above, when the statement at line 13 is called, it will call the default
constructor line 1. When statement in line 2 is executed, it will then call the constructor
that has a String parameter (in line 6).
There are a few things to remember when using the this constructor call:
1. When using the this constructor call, IT MUST OCCUR AS THE FIRST STATEMENT
in a constructor
2. It can ONLY BE USED IN A CONSTRUCTOR DEFINITION. The this call can then be
followed by any other relevant statements.
Introduction to Programming I 5
J.E.D.I.
Suppose we have the following implementation for the getName method in the Person
superclass,
So, when we invoke the getName method of an object of class Student, the overridden
method would be called, and the output would be,
Student: getName
Many of the classes in the Java API are declared final to ensure that their behavior
cannot be overridden. Examples of these classes are Integer, Double and String.
It is also possible in Java to create methods that cannot be overridden. These methods
are what we call final methods. To declare a method to be final, we just add the final
keyword in the method declaration. For example, if we want the getName method in
class Person to be declared final, we write,
Introduction to Programming I 6
J.E.D.I.
Static methods are automatically final. This means that you cannot override them.
Introduction to Programming I 7