Vous êtes sur la page 1sur 6

DPJ 5018 - Programming in Java

Chapter 5
July 2011

Chapter 5: OBJECT ORIENTED PROGRAMMING


PART 2: INHERITANCE AND POLYMOPHISM (Cont)
The protected Modifier

A protected data or a protected method in a public class can be accessed by :


o any class in the same package
o its subclasses, even if the subclasses are in a different package.
The symbol in UML diagram is #

POLYMOPHISM

Polymorphism is the capability of an action or method to do different things based on the


object that it is acting upon.
Polymorphism has two basic requirements, Method overriding and Method invocation
through a superclass reference.

Example 1: Using a superclass reference variable

As introduced in previous notes (C4 & C5), we create an object of class as below:

Cylinder c1 = new Cylinder ();


Code Snippet 5.4

It means c1 is Cylinder type and holds the address of Cylinder object

However, as Cylinder is extends from Circle, Java allows to declare as below:

Circle c1 = new Cylinder ();


Code Snippet 5.5

This statement declares c1 as a Circle variable and it creates a Cylinder object


and stores the objects address in the c1 variable.

This is correct and legal because a Cylinder object is also a Circle object.

This explains about polymorphism. The reference variable can refer to different type of
objects, as long as those types are its subclasses.

Page 1 of 6

as/5108/Tri47/C5p

DPJ 5018 - Programming in Java

Chapter 5
July 2011

Example 2: Using a polymorphic array and polymorphic method

Staff

Executive

PartTimer

Salesperson

Figure 5.2: A class hierarchy between Staff, Executive, PartTimer and Salesperson
class.
The following programs are to demonstrate the concept of overriding and polymorphism:
Staff is the superclass that defines basic info that a staff might has such name, id and
salary

Executive is a Staff and the salary is based on monthly pay.


PartTimer is a Staff who is paid based on the hours and rate per-hour. It overrides
getSalary from Staff.
Salesperson is a Staff that receives base salary with additional commission, which
is based on the monthly sales. It overrides getSalary from Staff.

Page 2 of 6

as/5108/Tri47/C5p

DPJ 5018 - Programming in Java

Chapter 5
July 2011

Program 5.5: Staff.java

Page 3 of 6

as/5108/Tri47/C5p

DPJ 5018 - Programming in Java

Chapter 5
July 2011

Program 5.6: Executive.java

Program 5.7: PartTimer.java

Program 5.8: Salesperson.java

Page 4 of 6

as/5108/Tri47/C5p

DPJ 5018 - Programming in Java

Chapter 5
July 2011

Program 5.9: Demo/Test Program

Explanations:
1) Program 5.9 demonstrates polymorphic behavior.
2) Refer to Line 5 - 9:

It declares an array of Staff variables, and then assigns the addresses of


objects of various types to the elements of the array. It is called polymorphic
array.

3) Refer to Line 18 - 22:

This method uses parameter to accept arguments to method polymorphically.

This methods parameter, s is a Staff variable. But it can be used to accept


arguments of any type that inherits from Staff.

Dynamic Binding

When a superclass variable contains a subclass object, a potential problem


exists. What if the subclass has overridden a method in the superclass, and
the variable makes a call to that method (superclasss)?

Refer to Program 5.8 (Line 12): The program overrides the getSalary method.

Refer to Program 5.9 (Line 21):

Page 5 of 6

as/5108/Tri47/C5p

DPJ 5018 - Programming in Java

Chapter 5
July 2011

i. In this case, the s variable references a subclass (e.g.: person[1] of

PartTimer) , so the PartTimers getSalary method is called.


This is called dynamic binding.

Java performs dynamic binding or late binding when a variable contains a


polymorphic reference (like person).

JVM determines at runtime which method to call, depending on the type of object
that the variables references.

AGGREGATION

Aggregation occurs when an instance of a class is a field in another class.


In another word, an object of a class is a member of another class.
The relationship to describe this is: has-a relationship.
To show aggregation in UML diagram by connecting two classes with a line that has an
open diamond at one end. The diamond is closest to the class that is the aggregate.
For example:
o Result class needs a data from Student class, Subject class and
Lecturer class.
o Therefore the object of Student, Subject and Lecturer is member (data
field) in Result class.
o This relationship of Result in this case is aggregation. Result has-a
Student, Subject, and Lecturer.
o UML diagram:

Result

Student

Subject

Lecturer

Figure 5.3: UML diagram that shows the relationship among the Result, Student, Subject and
Lecturer classes.

Page 6 of 6

as/5108/Tri47/C5p

Vous aimerez peut-être aussi