0% found this document useful (0 votes)
19 views

Polymorphism

This document discusses polymorphism in Java. It defines polymorphism as an object taking on multiple forms. In Java, polymorphism is achieved through inheritance, where a subclass can override or implement a parent class's methods. This allows a reference variable of a parent type to point to child class objects at runtime. The document provides examples of abstract classes and methods that must be implemented by concrete subclasses for polymorphism to work. It also discusses method overriding, late binding, and using instanceof to determine an object's actual type at runtime.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Polymorphism

This document discusses polymorphism in Java. It defines polymorphism as an object taking on multiple forms. In Java, polymorphism is achieved through inheritance, where a subclass can override or implement a parent class's methods. This allows a reference variable of a parent type to point to child class objects at runtime. The document provides examples of abstract classes and methods that must be implemented by concrete subclasses for polymorphism to work. It also discusses method overriding, late binding, and using instanceof to determine an object's actual type at runtime.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Polymorphism

By: Masurah Mohamad

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

6 CSC238, OOP Prepared by: Miss Masurah Mohamad


Subclass: StudentPoly

7 CSC238, OOP Prepared by: Miss Masurah Mohamad


Subclass: EmployeePoly

8 CSC238, OOP Prepared by: Miss Masurah Mohamad


Main class

9 CSC238, OOP Prepared by: Miss Masurah Mohamad


Operator instanceof
 Is used to determine the class of an object (which subclass is
currently point by object reference of superclass)
 Example
 To find out the number of students whose cgpa more than 3.5
Person p = new Student();
if(p instanceof Student){
System.out.println(“P is a student”); }
else {
System.out.println(“P is an employee”); } }

int student = 0;
for(int i=0; i<5<;i++) {
if(person[i] instanceof Student) {
student++; } }
10
Example: instanceof

11 CSC238, OOP Prepared by: Miss Masurah Mohamad


Example: instanceof (cont.)
instanceof

Use cast operator to make sure p[i] points


to an object of subclass EmployeePoly

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

22 CSC238, OOP Prepared by: Miss Masurah Mohamad


Method overriding : Example
2

23 CSC238, OOP Prepared by: Miss Masurah Mohamad


Method overriding : Example
3

g point to r
g called
toString()
and
display()
methods
owned by
Raya
class

24 CSC238, OOP Prepared by: Miss Masurah Mohamad


Concrete sub classes and methods
 Concrete subclasses: write all the definitions of abstract methods that have in the
abstract superclass and can be instantiated

25 CSC238, OOP Prepared by: Miss Masurah Mohamad


Exercise
 Write the definition for the super and subclass below:
 Super class: Car (abstract)
 2 attributes:(name and color)
 2 abstract methods: (display:void and calculate_price:double)
 Sub class: Perodua (non abstract)
 2 attributes: price and tax
 Overrides abstract methods (display() and calculate price())
 display() : to display the information of perodua cars including total
price
 calculate_price() : to calculate total price of car after adding tax into
car price.
 Main method: test the program to display the output.

26 CSC238, OOP Prepared by: Miss Masurah Mohamad

You might also like