Presented by: Mohammad Mustafa Ibrahimy
Lecturer of Computer Science Department, Takhar University
Size: 479.68 KB
Language: en
Added: Sep 28, 2018
Slides: 20 pages
Slide Content
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Polymorphism in OOP
Supervisor: Prof. Abdul Rahman Mujadadi
Presentor: M. Mustafa Ibrahimy
18, Oct, 2017
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Contents
•Introduction
•What is Polymorphism?
•Advantages & Disadvantages
•Types of Polymorphisms
•Conclusion
2
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Aim of Today’s Seminar
•To introduce polymorphismin OOP
3
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
4
-ItisoneoftheimportantconceptofOOP
-Withpolymorphismcandesign,implement
systemswhichareextensible.
-Newclassescanbeaddedwithlittleorwithout
modificationingeneralportionsoftheprogram
[3].
Introduction
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
What is Polymorphism?
Definitions:
•The process of representing one form in multiple
forms is known asPolymorphism [2].
•Theabilitytotakemorethanoneformiscalled
polymorphism[1].
•Polymorphism is derived from 2 Greek
words:polyandmorphs. It means many
forms[2].
5
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Real life example of polymorphism
6
[2]
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Important Points About Polymorphism
•Thebehaviordependsonthetypeofdatausedin
theoperation
•Polymorphismisusedforimplementing
inheritance.[4]
7
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Advantages Of Polymorphism
•Ithelpsprogrammersreusethecodeandclasses
oncewritten,testedandimplemented.Theycanbe
reusedinmanyways.
•Singlevariablenamecanbeusedtostorevariables
ofmultipledatatypes(Float,double,Long,Int
etc)[5].
8
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Disadvantages Of Polymorphism
•developers find it difficult to implement
polymorphism in codes.
•Polymorphism reduces the readability of the
program[5].
9
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Types of Polymorphism
Polymorphismis divided into two types:
1)Method overloading (compile time)
It is also known asStatic binding, Early binding
2)Method overriding (run time)
It is also known asDynamic binding, Late binding.
10
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Continue…
Staticbinding:happensatcompiletime
Dynamicbinding:happensatruntime
DynamicBinding:istheprocessoflinking
procedurecalltoaspecificsequenceofcode
(method)atrun-time.Itmeansthatthecodetobe
executedforaspecificprocedurecallisnotknown
untilrun-time[5].
11
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Method Overloading
•Inmethodoverloading,aclasshasmultiple
methodswithsamenamebutdifferent
parameters.
•Wecanachievemethodoverloadingbychanging
parametertypesorbychangingthenumberof
parameters[4].
12
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Method overloading -Example
public class OverLoading{
static class Mthdoverloading{
void test() {
System.out.println("No parameters");
}
//Overload test for one integer parameter.
void test(inta) {
System.out.println("a: "+a);
}
//Overload test for two integer parameters.
void test(inta, intb) {
System.out.println("a and b: " + a + " " +b);
}
} 13
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Continue…
public static void main(String[] args) {
Mthdoverloadingob= new
Mthdoverloading();
ob.test();
ob.test(10);
ob.test(10,20);
}
}
Output:
No parameters
a: 10
a and b: 10 20
14
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Method Overriding
15
•Inmethodoverriding,superclassandsub
classhavingmethodswithsamenameand
sameparametersbuthavingdifferent
implementations[4].
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Method Overriding -Example
16
public class Overriding {
static class Animal {
public void move() {
System.out.println("Animals can move");
}
}
static class Dog extends Animal {
@Override
public void move() {
//invokes the super class method
System.out.println("Dogs can walk and run");
}
}
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Continue…
17
static class Bird extends Animal {
@Override
public void move() {
//invokes the super class method
System.out.println("Birds can fly");
}
}
public static void main(String[] args) {
Dog obj= new Dog();
obj.move();
Bird b = new Bird();
b.move();
}
}
Output:
Dogs can walk and run
Birds can fly
Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Conclusion
Polymorphism:
-AnimportantOOPconcept
-Actdifferentlyunderdifferentcontext.
Methodoverloadingincreasesthereadabilityofthe
program.
Methodoverridingisusedtoachieverun-time
polymorphism.
18