Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views8 pages

Chapter 5 - Inheritance and Polymorphism

Chapter 5 of the document discusses inheritance in Java, explaining the concepts of superclasses and subclasses, and how methods and properties can be inherited. It covers the use of the 'extends' keyword, the 'super' keyword for constructor calls, method overriding, and the declaration of final classes and methods. Additionally, it introduces polymorphism, demonstrating how a superclass reference can point to subclass objects and invoke the appropriate overridden methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Chapter 5 - Inheritance and Polymorphism

Chapter 5 of the document discusses inheritance in Java, explaining the concepts of superclasses and subclasses, and how methods and properties can be inherited. It covers the use of the 'extends' keyword, the 'super' keyword for constructor calls, method overriding, and the declaration of final classes and methods. Additionally, it introduces polymorphism, demonstrating how a superclass reference can point to subclass objects and invoke the appropriate overridden methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

[COMPUTER PROGRAMMING 2] IT 103

CHAPTER 5
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.

Figure 1: Class Hierarchy


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.

Saint Louis College – College of Information Technology Page 1


[COMPUTER PROGRAMMING 2] IT 103

Defining Superclasses and Subclasses


To derive a class, we use the extends keyword. In order to illustrate this, let's 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 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.

Saint Louis College – College of Information Technology Page 2


[COMPUTER PROGRAMMING 2] IT 103

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.

Figure 2: Program Flow


Saint Louis College – College of Information Technology Page 3
[COMPUTER PROGRAMMING 2] IT 103

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.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:

 The super() call MUST OCCUR THE FIRST STATEMENT IN A CONSTRUCTOR.


 The super() call can only be used in a constructor definition.
 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”;
}

Saint Louis College – College of Information Technology Page 4


[COMPUTER PROGRAMMING 2] IT 103

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.println("Parent: getName");
return name;
}
:
}

To override, the getName method in the subclass Student, we write,

public class Student extends Person


{
:
:
public String getName(){
System.out.println("Student: 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

Saint Louis College – College of Information Technology Page 5


[COMPUTER PROGRAMMING 2] IT 103

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.

Saint Louis College – College of Information Technology Page 6


[COMPUTER PROGRAMMING 2] IT 103

Polymorphism
Now, given the parent class Person and the subclass Student of our previous example,
we add another subclass of Person which is Employee.

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;
}
}
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 )

Saint Louis College – College of Information Technology Page 7


[COMPUTER PROGRAMMING 2] IT 103

{
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 ){


....
}

Saint Louis College – College of Information Technology Page 8

You might also like