Polymorphism
Polymorphism
1
Scope
Polymorphism concept
Abstract classes and methods
Method overriding
Concrete sub classes and methods
Array of super classes
2
Polymorphism concept
Definition
The ability to appear in many forms
Polymorphism in Java program
The ability of reference variable to change behavior
according to what object instance it is holding
Allows multiple objects of different subclasses to be
treated as objects of single superclass, while
automatically selecting the proper methods to apply to
a particular object based on the subclass it belongs to.
In other words, a reference variable of a superclass
type can point to an object of its subclass
3
Polymorphism concept
Is implemented using late binding
2 types of binding
Early binding – a method’s definition is
associated with its invocation when the code is
compiled
Late binding – a method’s definition is
associated with the method’s invocation at
execution time (when the method is executed)
Associating a method definition with its
invocation = determining which method
4
definition gets executed
Example of polymorphism
Class Person hierarchy
5
Superclass : PersonPoly
int student = 0;
for(int i=0; i<5<;i++) {
if(person[i] instanceof Student) {
student++; } }
10
Example: instanceof
12
Abstract classes and methods
Abstract Class
A class that is declared with the reserved word
abstract in its heading
A class that is never instantiated. Its purpose is to be
a parent to several related classes. The child classes
inherit from the abstract parent class
Can contain instance variables, constructors, the
finalizer, and non abstract methods
Can contain an abstract method(s)
Finalizer is a void method. A class can have only 1 finalizer. Finalizer can’t have any
13
parameters.
CSC238, Name of
OOP Prepared by: finalizer is finalize.
Miss Masurah MohamadFinalize automatically executes when class object goes
out of scope (to free up the memory allocated by the object of the class)
Abstract classes and methods
Abstract Class (cont.)
If a class contains an abstract method, then the class
must be declared abstract
You cannot instantiate an object of an abstract class.
You can only declare a reference variable of an
abstract class type
You can instantiate an object of a subclass of an
abstract class, but only if the subclass gives the
definitions of all the abstract methods of the superclass
14
Abstract classes and methods
Abstract method
Method that has only the heading with no body
The heading of an abstract method contains the
reserved word abstract and ends with a semicolon
It declares an access modifier, return type, and method
signature followed by a semicolon
Example:
public void abstract print();
public abstract object_larger (object, object);
void abstract insert (int insertItem);
15
Abstract classes and methods
A non-abstract child class inherits the abstract
method and must define a non-abstract method
that matches the abstract method.
An abstract child of an abstract parent does not
have to define non-abstract methods for the
abstract signatures it inherits.
Abstract classes and methods
Example
abstract class Card
{
String recipient; // name of who gets the card
public abstract void greeting(); // abstract greeting()
method
}
The abstract class can include abstract methods and non-abstract methods. If a class
contains even one abstract method, then the class itself has to be declared to be abstract.
Abstract classes and methods
class Holiday extends Card
{ public Holiday( String r )
{ recipient = r; }
public void greeting()
greeting()
{ method is
defined
System.out.println("Dear “ + recipient + ",\n"); by
subclass
System.out.println("Season's Greetings!\n\n"); Holiday
}
Holiday is a non abstract child of an abstract parent. Objects can be instantiated
} from it.
Abstract classes and methods
explanation on Holiday subclass
Holiday inherits the abstract method greeting()
from its parent.
Holiday must define a greeting() method that
includes a method body (statements between
braces).
The definition of greeting() must match the
signature given in the parent.
If Holiday did not define greeting(), then Holiday
would be declared an abstract class.
This would make it an abstract child of an
abstract parent.
Abstract classes and methods
Advantage of using an abstract class
You can group several related classes together
as siblings.
To keep a program organized and
understandable.
Abstract classes and methods :
Method overriding
A non-abstract child must override each abstract
method inherited (all abstract methods) from its
parent by defining a method with the same
signature and same return type.
This is called concrete subclasses, used to emphasize the
fact that it is not abstract.
A child may define additional methods with
signatures different from the parent's method.
It is an error if a child defines a method with the
same signature as a parent method, but with a
different return type.
Method overriding : Example
1
g point to r
g called
toString()
and
display()
methods
owned by
Raya
class