Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Polymorphism in oop
1. 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
2. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Contents
• Introduction
• What is Polymorphism?
• Advantages & Disadvantages
• Types of Polymorphisms
• Conclusion
2
3. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Aim of Today’s Seminar
• To introduce polymorphism in OOP
3
4. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
4
- It is one of the important concept of OOP
- With polymorphism can design, implement
systems which are extensible.
- New classes can be added with little or without
modification in general portions of the program
[3].
Introduction
5. 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 as Polymorphism [2].
• The ability to take more than one form is called
polymorphism [1].
• Polymorphism is derived from 2 Greek
words: poly and morphs. It means many
forms[2].
5
6. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Real life example of polymorphism
6
[2]
7. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Important PointsAbout Polymorphism
• The behavior depends on the type of data used in
the operation
• Polymorphism is used for implementing
inheritance. [4]
7
8. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Advantages Of Polymorphism
• It helps programmers reuse the code and classes
once written, tested and implemented. They can be
reused in many ways.
• Single variable name can be used to store variables
of multiple data types(Float, double, Long, Int
etc) [5].
8
9. 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
10. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Types of Polymorphism
Polymorphism is divided into two types:
1) Method overloading (compile time)
It is also known as Static binding, Early binding
2) Method overriding (run time)
It is also known as Dynamic binding, Late binding.
10
11. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Continue…
Static binding: happens at compile time
Dynamic binding: happens at runtime
Dynamic Binding: is the process of linking
procedure call to a specific sequence of code
(method) at run-time. It means that the code to be
executed for a specific procedure call is not known
until run-time [5].
11
12. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Method Overloading
• In method overloading, a class has multiple
methods with same name but different
parameters.
• We can achieve method overloading by changing
parameter types or by changing the number of
parameters [4].
12
13. 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(int a) {
System.out.println("a: "+a);
}
//Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " +b);
}
} 13
14. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Continue…
public static void main(String[] args) {
Mthdoverloading ob= new
Mthdoverloading();
ob.test();
ob.test(10);
ob.test(10,20);
}
}
Output:
No parameters
a: 10
a and b: 10 20
14
15. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Method Overriding
15
• In method overriding, super class and sub
class having methods with same name and
same parameters but having different
implementations [4].
16. 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");
}
}
17. 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
18. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
Conclusion
Polymorphism:
- An important OOP concept
- Act differently under different context.
Method overloading increases the readability of the
program.
Method overriding is used to achieve run-time
polymorphism.
18
19. Computer Science Faculty/Kabul University
Polymorphism in Object Oriented Programming (OOP)
References
[1] T.P, Abbas. (2010). Data Structures & OOP Using C++,
Kerala: Calicut University Central Co-operative Store
[2] Kumar, Hitesh.(2013). Java Polymorphism. Retrieved 2017,
Oct 14, from https://www.sitesbay.com
[3] Oruji, Amin.(2013, December). OOP-Polymorphism.
Retrieved 2017, Oct 14, from https://www.oruji.org
[4] Christensson, P. (2007, November 2). OOP Definition.
Retrieved 2017, Oct 14, from https://techterms.com
[5] Sparrow, Penna.(2010, June). OOP-Polymorphism.
Retrieved 2017, Oct 17, from http://www.ianswer4u.com
19