J.E.D.I
11  Inheritance, Polymorphism and Interfaces
11.1  Objectives
In  this   section,  we  will   be  discussing   on  how  a   class can  inherit   the  properties  of  an existing class. A class that does this is called a subclass and its parent class is called the
superclass.   We   will   also   be   discussing   a   special   property   of   Java   wherein   it   can automatically apply the proper methods to each object regardless of what subclass the
object   came   from.  This   property   is   known   as   polymorphism.   Finally,   we  are   going   to discusss about interfaces that helps reduce programming effort.
At the end of the lesson, the student should be able to: • Define super classes and subclasses
• Override methods of superclasses • Create final methods and final classes
11.2  Inheritance
In Java, all classes, including the classes that make up the Java API, are subclassed from the Object superclass. A sample class hierarchy is shown below.
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.
Inheritance is a major advantage in object-oriented programming since once a behavior method   is   defined   in   a   superclass,   that   behavior   is   automatically   inherited   by   all
subclasses.   Thus,   you   can   encode   a   method   only   once   and   they   can   be   used   by   all subclasses. A subclass only  need  to  implement  the differences  between  itself and  the
parent.
Introduction to Programming I 170
Figure 11.1: Class Hierarchy
J.E.D.I
11.2.1  Defining Superclasses and Subclasses
To derive a class, we use the extends keyword. In order to illustrate this, lets create a sample parent class. Suppose we have a parent class called Person.
public class Person {
protected String  name; protected String
address; 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; }
public String getAddress{ return address;
} public void setName String name {
this.name = name; }
public void setAddress String add { this.address = add;
} }
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 wont 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 171
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,
public class Student extends Person {
public Student{ System.out.println“Inside Student:Constructor”;
some code here }
some code here }
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,
public static void main String[] args { Student anna = new Student;
} In the code, we create an object of class Student. The output of the program is,
Inside Person:Constructor Inside Student:Constructor
The program flow is shown below.
Introduction to Programming I 172
Figure 11.2: Program Flow
J.E.D.I
11.2.2  The super keyword
A subclass can also explicitly call a constructor of its immediate superclass. This is done by   using   the  super  constructor   call.   A   super   constructor   call   in   the   constructor   of   a
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.printlnInside 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.printlnInside 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:
1. The super call MUST OCCUR THE FIRST STATEMENT IN A CONSTRUCTOR. 2. The super call can only be used in a constructor definition.
3. This implies that the this construct and the super calls CANNOT BOTH OCCUR IN THE SAME CONSTRUCTOR.
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”;
}
Introduction to Programming I 173
J.E.D.I
11.2.3  Overriding Methods
If for some reason a derived class needs to have a different implementation of a certain method from that of the superclass, overriding methods could prove to be very useful.
A   subclass   can   override   a   method   defined   in   its   superclass   by   providing   a   new implementation for that method.
Suppose we have the following implementation for the getName method in the Person superclass,
public class Person {
: :
public String getName{ System.out.printlnParent: getName;
return name; }
: }
To override, the getName method in the subclass Student, we write, public class Student extends Person
{ :
: public String getName{
System.out.printlnStudent: getName; return name;
} :
} 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
Introduction to Programming I 174
J.E.D.I
11.2.4  Final Methods and Final Classes
In Java, it is also possible to declare classes that can no longer be subclassed. These classes   are  called  final  classes.  To  declare  a   class to  be  final,   we  just   add  the  final
keyword in the class declaration. For example, if we want the class Person to be declared final, we write,
public final class Person {
some code here }
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,
public final String getName{
return name; }
Static methods are automatically final. This means that you cannot override them.
Introduction to Programming I 175
J.E.D.I
11.3  Polymorphism
Now, given the parent class Person and the subclass Student of our previous example, we add another subclass of Person which is Employee. Below is the class hierarchy for
that,
In Java, we can create a reference that is of type superclass to an object of its subclass. For example,
public static main String[] args {
Person ref;
Student studentObject = new Student;
Employee employeeObject = new Employee;
ref = studentObject; Person ref points to a Student object
some code here }
Now suppose we have a getName method in our superclass Person, and we override this method in both the subclasses Student and Employee,
public class Person {
public String getName{ System.out.println“Person Name:” + name;
return name; }
} public class Student extends Person
{ public String getName{
System.out.println“Student Name:” + name; return name;
} }
public class Employee extends Person {
public String getName{ System.out.println“Employee Name:” + name;
return name; }
}
Introduction to Programming I 176
Figure 11.3: Hierarchy for Person class and its classes
Person
Student Employee
J.E.D.I
Going   back   to   our   main   method,   when   we   try   to   call   the   getName   method   of   the reference Person ref, the getName method of the Student object will be called. Now, if
we assign ref to an Employee object, the getName method of Employee will be called.
public static main String[] args {
Person ref;
Student studentObject = new Student;
Employee employeeObject = new Employee;
ref = studentObject; Person reference points to a Student object
String temp = ref.getName; getName of Student class is called
System.out.println temp ;
ref = employeeObject; Person reference points to an Employee object
String temp = ref.getName; getName of Employee class is called
System.out.println temp ;
} This ability of our reference to change behavior according to what object it is holding is
called  polymorphism. Polymorphism allows multiple objects of different subclasses to be   treated   as   objects   of   a   single   superclass,   while   automatically   selecting   the   proper
methods to apply to a particular object based on the subclass it belongs to.
Another example that exhibits the property of polymorphism is when we try to pass a reference to methods. Suppose we have a static method printInformation that takes in
a Person object as reference, we can actually pass a reference of type Employee and type Student to this method as long as it is a subclass of the class Person.
public static main String[] args {
Student studentObject = new Student;
Employee employeeObject = new Employee;
printInformation studentObject ; printInformation employeeObject ;
}
public static printInformation Person p {
. . . . }
Introduction to Programming I 177
J.E.D.I
11.4  Abstract Classes