[go: up one dir, main page]

0% found this document useful (0 votes)
5 views15 pages

04 Polymorphism

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

04 Polymorphism

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CS3007D OOS

Polymorphism, Abstract Classes and


Interfaces
Outline

• INTRODUCTION
• MAJOR CLASSES OF POLYMORPHISM
• ANOTHER CLASSIFICATION
• EXAMPLE BASED ON OVERRIDING REVISITED
• SUMMARY
Polymorphism - Introduction

• Literally means many forms


• Single symbol represent multiple different
meanings
• Same symbol can exhibit different actions
based on the context
• One of the important feature of Object
Oriented Systems
Major classes of Polymorphism

• Overloading (Adhoc Polymorphism)


• Overriding (Inclusion Polymorphism)
• Polymorphism based on Subtyping
(Assignment Polymorphism)
• Parametric Polymorphism
Overloading
• Also termed as Adhoc Polymorphism
• Compile time Polymorphism
• Mainly used as
– Function (method) Overloading
– Operator Overloading
Function or Method Overloading

● Ability to create multiple functions of the same name


with different implementations.
● Overloading allows a programmer to use the same
method name over and over, as long as the signature of
the method is different each time

Eg : public String getRecord(int key)

Signature is Method name + Parameter list


Function overloading - example

public void getCab() public int sum (int a, int b)


{.......} {....}
public int sum (int a, int b, int c)
public void getCab (String cabbieName);
{......}
{.......}
public void getCab (int numberOfPassengers);
{.......}
Constructor overloading - example

public class Count {

int count;
public Count()

{ count = 0; }

public Count (int number)

{ count = number; }

}
Overriding
• Occurs with in the context of parent class- child class
relationship
• Must have the same type signature
• Run time polymorphism
Polymorphism based on Subtyping
• With the support of Subclassing or Inheritance

class Animal {
String speak() { return “Hi”; }}

class Cat extends Animal{


String speak() { return “Meow”; }}

class Dog extend Animal{


String speak() { return “Bow Bow”; }}

Animal obj1 = new Dog();


Animal obj2 = new Cat();
Animal obj3 = new Animal();
Parametric Polymorphism
• Generics

class Node<T>
{
T elem;
Node<T> next;
}

Node<Integer> intObject = new Node<Integer>();


Another Classification

• Based on when the implementation is


selected
• Compile time Polymorphism
• Runtime polymorphism
Overriding and Dynamic Binding
Revisited

Student listStudent [40];


listStudent[0]= new UnderGraduateStudent();
listStudent[1]= new GraduateStudent();
...
Summary
• Polymorphism is the capability for a symbol
to be interpreted in different forms
• Overloading, overriding, Parametric and
Subtyping are different variants of
polymorphism
• Overriding or Polymorphism based on
inheritance is one of the important feature of
OOP
THANK YOU

You might also like