SlideShare a Scribd company logo
Object Oriented
Programming
Andi Nurkholis, S.Kom., M.Kom.
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2019-2020
April 23, 2020
7.2 Polymorphism
2
Inheritance in Java
Polymorphism means "many forms", and it occurs when we have many
classes that are related to each other by inheritance.
Like we specified in the previous chapter; Inheritance lets us inherit
attributes and methods from another class. Polymorphism uses those
methods to perform different tasks. This allows us to perform a single
action in different ways.
3
4
Inheritance in Java (cont.)
For example, think of a superclass called Animal that has a method called
Sound(). Subclasses of Animals could be Cats, Dogs, Birds - And they
also have their own implementation of an animal sound (the cat meow
and the dog bark, etc.):
Example
5
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
Now we can create Animal as Super Class and make the animalSound()
method:
Example (cont.)
Then we can create Cat as Sub Class of Animal and make a new different
animalSound() method:
6
class Cat extends Animal {
public void animalSound() {
System.out.println("The cat says: meow meow");
}
}
Example (cont.)
7
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bark bark");
}
}
Then we can create Dog as Sub Class of Animal and make a new different
animalSound() method:
Example (cont.)
8
class MyMainClass {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal d1 = new Cat();
Animal c1 = new Dog();
myAnimal.animalSound(); //same function name
d1.animalSound(); //same function name
c1.animalSound(); //same function name
}
}
Now we can create Cat and Dog objects and call the animalSound()
method on both of them:
Explanation
9
In previous slide, we have three same function names from three different
classes that related each other.
What the meaning?
Polymorphism means same function name (but different signatures) being
uses for different types.
Types of Polymorphism
In Java polymorphism is mainly divided into two types:
• Run-time Polymorphism
• Compile-time Polymorphism
10
11
Run-time Polymorphism
In Java, run-time polymorphism can be achieved through method
overriding.
Suppose the same method is created in the superclass and its
subclasses. In this case, the method that will be called depends upon the
object used to call the method.
Method overriding, on the other hand, occurs when a derived class has a
definition for one of the member functions of the base class. That base
function is said to be overridden.
Illustration
In previous example (p. 5-8), that is an example of run-time polymorphism.
Lets check the illustration:
12
Explanation
13
In the previous illustration, the method makeSound() has different
implementations in two different classes. When we run the program,
• The expression d1.makeSound() will call the method of Dog class. It is
because d1 is an object of the Dog class.
• The expression c1.makeSound() will call the method of Cat class. It is
because c1 is an object of the cat class.
Compile-time Polymorphism
The compile-time polymorphism can be achieved through method
overloading and operator overloading in Java.
Method Overloading, there are multiple functions with same name but
different parameters then these functions are said to be overloaded.
Functions can be overloaded by change in number of arguments or/and
change in type of arguments. For example:
14
void func() { ... }
void func(int a) { ... }
float func(double a) { ... }
float func(int a, float b) { ... }
15
Example
class Demo {
public void displayPattern(){
for(int i = 0; i < 10; i++) {
System.out.print("*");
}
}
public void displayPattern(char symbol) {
for(int i = 0; i < 10; i++) {
System.out.print(symbol);
}
}
}
16
Example (cont.)
class Main {
public static void main(String[] args) {
Demo d1 = new Demo();
d1.displayPattern();
System.out.println("n");
d1.displayPattern('#');
}
}
//output **********
##########
Explanation
In previous example, the displayPattern()
method is overloaded.
• If we call the method without passing any
arguments, a pattern of * is created.
• If we call the method by passing a character
as an argument, a pattern of that character is
created.
17
Overloading Vs Overriding
• In the case of method overriding, methods should be inside different
classes. Whereas, in the case of method overloading, methods should
be inside the same class.
• Method overriding is performed at run-time whereas method
overloading is performed at compile-time.
18
So, Why “Polymorphism"?
Polymorphism allows us to create consistent code. For example,
Suppose we need to render a circle and a square. To do so, we can
create a Polygon class and inherited two subclasses Circle and Square
from it. In this case, it makes sense to create a method having the same
name render() in both these subclasses rather than creating methods
with different names.
In previous method overloading example, we have used the same
method name displayPattern() to display two different patterns for
consistency.
19
Thank You, Next …
Encapsulation
Study Program of Informatics
Faculty of Engineering and Computer Science
SY. 2019-2020
Andi Nurkholis, S.Kom., M.Kom.
April 23, 2020
Ad

More Related Content

What's hot (20)

Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23
myrajendra
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
NaBL: A Meta-Language for Declarative Name Binding and Scope Rules
NaBL: A Meta-Language for Declarative Name Binding  and Scope RulesNaBL: A Meta-Language for Declarative Name Binding  and Scope Rules
NaBL: A Meta-Language for Declarative Name Binding and Scope Rules
Eelco Visser
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
manish kumar
 
Intro to Functions Python
Intro to Functions PythonIntro to Functions Python
Intro to Functions Python
primeteacher32
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch
岳華 杜
 
Advanced Python : Static and Class Methods
Advanced Python : Static and Class Methods Advanced Python : Static and Class Methods
Advanced Python : Static and Class Methods
Bhanwar Singh Meena
 
Core java oop
Core java oopCore java oop
Core java oop
Parth Shah
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
Eduardo Bergavera
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
Michael Heron
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 
Effective Java - Enum and Annotations
Effective Java - Enum and AnnotationsEffective Java - Enum and Annotations
Effective Java - Enum and Annotations
Roshan Deniyage
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
kishu0005
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
Hashim Hashim
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
manish kumar
 
Super keyword.23
Super keyword.23Super keyword.23
Super keyword.23
myrajendra
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
NaBL: A Meta-Language for Declarative Name Binding and Scope Rules
NaBL: A Meta-Language for Declarative Name Binding  and Scope RulesNaBL: A Meta-Language for Declarative Name Binding  and Scope Rules
NaBL: A Meta-Language for Declarative Name Binding and Scope Rules
Eelco Visser
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
manish kumar
 
Intro to Functions Python
Intro to Functions PythonIntro to Functions Python
Intro to Functions Python
primeteacher32
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch
岳華 杜
 
Advanced Python : Static and Class Methods
Advanced Python : Static and Class Methods Advanced Python : Static and Class Methods
Advanced Python : Static and Class Methods
Bhanwar Singh Meena
 
Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
Eduardo Bergavera
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
Michael Heron
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 
Effective Java - Enum and Annotations
Effective Java - Enum and AnnotationsEffective Java - Enum and Annotations
Effective Java - Enum and Annotations
Roshan Deniyage
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
kishu0005
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
manish kumar
 

Similar to Object Oriented Programming - 7.2. Polymorphism (20)

Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
RiturajJain8
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
Janu Jahnavi
 
Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)
Geekster
 
Week 8,9,10 of lab of data communication .pdf
Week 8,9,10 of lab of data communication .pdfWeek 8,9,10 of lab of data communication .pdf
Week 8,9,10 of lab of data communication .pdf
nimrastorage123
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
Suresh Mohta
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
ilias ahmed
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
Vijaykota11
 
Design patterns(red)
Design patterns(red)Design patterns(red)
Design patterns(red)
Fahad A. Shaikh
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
 
polymorphism method overloading and overriding .pptx
polymorphism method overloading  and overriding .pptxpolymorphism method overloading  and overriding .pptx
polymorphism method overloading and overriding .pptx
thamaraiselvangts441
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
025466482929 -OOP with Java Development Kit.ppt
025466482929 -OOP with Java Development Kit.ppt025466482929 -OOP with Java Development Kit.ppt
025466482929 -OOP with Java Development Kit.ppt
DakshinaPahan
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
EmanAsem4
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear
ASHNA nadhm
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
kristinatemen
 
An Introduction to C# and .NET Framework (Basic)
An Introduction to C# and .NET Framework (Basic)An Introduction to C# and .NET Framework (Basic)
An Introduction to C# and .NET Framework (Basic)
Khubaib Ahmad Kunjahi
 
Java oop concepts
Java oop conceptsJava oop concepts
Java oop concepts
Syeful Islam
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
Shubham Sharma
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
Janu Jahnavi
 
Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)
Geekster
 
Week 8,9,10 of lab of data communication .pdf
Week 8,9,10 of lab of data communication .pdfWeek 8,9,10 of lab of data communication .pdf
Week 8,9,10 of lab of data communication .pdf
nimrastorage123
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
Suresh Mohta
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
ilias ahmed
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
Vijaykota11
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
polymorphism method overloading and overriding .pptx
polymorphism method overloading  and overriding .pptxpolymorphism method overloading  and overriding .pptx
polymorphism method overloading and overriding .pptx
thamaraiselvangts441
 
Inheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptxInheritance_Polymorphism_Overloading_overriding.pptx
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
025466482929 -OOP with Java Development Kit.ppt
025466482929 -OOP with Java Development Kit.ppt025466482929 -OOP with Java Development Kit.ppt
025466482929 -OOP with Java Development Kit.ppt
DakshinaPahan
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
EmanAsem4
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear
ASHNA nadhm
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
kristinatemen
 
An Introduction to C# and .NET Framework (Basic)
An Introduction to C# and .NET Framework (Basic)An Introduction to C# and .NET Framework (Basic)
An Introduction to C# and .NET Framework (Basic)
Khubaib Ahmad Kunjahi
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
Shubham Sharma
 
Ad

More from AndiNurkholis1 (20)

Pengantar Bisnis - 11 Kebijakan Distribusi
Pengantar Bisnis - 11 Kebijakan DistribusiPengantar Bisnis - 11 Kebijakan Distribusi
Pengantar Bisnis - 11 Kebijakan Distribusi
AndiNurkholis1
 
Technopreneurship - 8 Manajemen Sumber Daya Manusia
Technopreneurship - 8 Manajemen Sumber Daya ManusiaTechnopreneurship - 8 Manajemen Sumber Daya Manusia
Technopreneurship - 8 Manajemen Sumber Daya Manusia
AndiNurkholis1
 
Pengantar Bisnis - 10 Kebijakan Produk
Pengantar Bisnis - 10 Kebijakan ProdukPengantar Bisnis - 10 Kebijakan Produk
Pengantar Bisnis - 10 Kebijakan Produk
AndiNurkholis1
 
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
Technopreneurship - 7 Manajemen Pemasaran dan Operasional BisnisTechnopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
AndiNurkholis1
 
Pengantar Bisnis - 9 Manajemen Pemasaran
Pengantar Bisnis - 9 Manajemen PemasaranPengantar Bisnis - 9 Manajemen Pemasaran
Pengantar Bisnis - 9 Manajemen Pemasaran
AndiNurkholis1
 
Technopreneurship - 6 Business Plan
Technopreneurship - 6 Business PlanTechnopreneurship - 6 Business Plan
Technopreneurship - 6 Business Plan
AndiNurkholis1
 
Pengantar Bisnis - 8 Kepemimpinan
Pengantar Bisnis - 8 KepemimpinanPengantar Bisnis - 8 Kepemimpinan
Pengantar Bisnis - 8 Kepemimpinan
AndiNurkholis1
 
Technopreneurship - 5 Model Bisnis
Technopreneurship - 5 Model BisnisTechnopreneurship - 5 Model Bisnis
Technopreneurship - 5 Model Bisnis
AndiNurkholis1
 
Technopreneurship - 4 Studi Kelayakan Usaha
Technopreneurship - 4 Studi Kelayakan UsahaTechnopreneurship - 4 Studi Kelayakan Usaha
Technopreneurship - 4 Studi Kelayakan Usaha
AndiNurkholis1
 
Pengantar Bisnis - 7 Motivasi Kerja
Pengantar Bisnis - 7 Motivasi KerjaPengantar Bisnis - 7 Motivasi Kerja
Pengantar Bisnis - 7 Motivasi Kerja
AndiNurkholis1
 
Pengantar Bisnis - 6 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 6 Manajemen Sumber Daya ManusiaPengantar Bisnis - 6 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 6 Manajemen Sumber Daya Manusia
AndiNurkholis1
 
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian BisnisPengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
AndiNurkholis1
 
Technopreneurship - 3 Ide dan Prinsip Bisnis
Technopreneurship - 3 Ide dan Prinsip BisnisTechnopreneurship - 3 Ide dan Prinsip Bisnis
Technopreneurship - 3 Ide dan Prinsip Bisnis
AndiNurkholis1
 
Pengantar Bisnis - 4 Bentuk Organisasi Bisnis
Pengantar Bisnis - 4 Bentuk Organisasi BisnisPengantar Bisnis - 4 Bentuk Organisasi Bisnis
Pengantar Bisnis - 4 Bentuk Organisasi Bisnis
AndiNurkholis1
 
Technopreneurship - 2 Pengantar Technopreneurship
Technopreneurship - 2 Pengantar TechnopreneurshipTechnopreneurship - 2 Pengantar Technopreneurship
Technopreneurship - 2 Pengantar Technopreneurship
AndiNurkholis1
 
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis InternasionalPengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
AndiNurkholis1
 
Pengantar Bisnis - 2 Bisnis dan Lingkungan Bisnis
Pengantar Bisnis - 2 Bisnis dan Lingkungan BisnisPengantar Bisnis - 2 Bisnis dan Lingkungan Bisnis
Pengantar Bisnis - 2 Bisnis dan Lingkungan Bisnis
AndiNurkholis1
 
Technopreneurship - 1 Kontrak Perkuliahan
Technopreneurship - 1 Kontrak PerkuliahanTechnopreneurship - 1 Kontrak Perkuliahan
Technopreneurship - 1 Kontrak Perkuliahan
AndiNurkholis1
 
Pengantar Bisnis - 1 Kontrak Perkuliahan
Pengantar Bisnis - 1 Kontrak PerkuliahanPengantar Bisnis - 1 Kontrak Perkuliahan
Pengantar Bisnis - 1 Kontrak Perkuliahan
AndiNurkholis1
 
Pemrograman Aplikasi Mobile - 11 Mobile Security
Pemrograman Aplikasi Mobile - 11 Mobile SecurityPemrograman Aplikasi Mobile - 11 Mobile Security
Pemrograman Aplikasi Mobile - 11 Mobile Security
AndiNurkholis1
 
Pengantar Bisnis - 11 Kebijakan Distribusi
Pengantar Bisnis - 11 Kebijakan DistribusiPengantar Bisnis - 11 Kebijakan Distribusi
Pengantar Bisnis - 11 Kebijakan Distribusi
AndiNurkholis1
 
Technopreneurship - 8 Manajemen Sumber Daya Manusia
Technopreneurship - 8 Manajemen Sumber Daya ManusiaTechnopreneurship - 8 Manajemen Sumber Daya Manusia
Technopreneurship - 8 Manajemen Sumber Daya Manusia
AndiNurkholis1
 
Pengantar Bisnis - 10 Kebijakan Produk
Pengantar Bisnis - 10 Kebijakan ProdukPengantar Bisnis - 10 Kebijakan Produk
Pengantar Bisnis - 10 Kebijakan Produk
AndiNurkholis1
 
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
Technopreneurship - 7 Manajemen Pemasaran dan Operasional BisnisTechnopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
Technopreneurship - 7 Manajemen Pemasaran dan Operasional Bisnis
AndiNurkholis1
 
Pengantar Bisnis - 9 Manajemen Pemasaran
Pengantar Bisnis - 9 Manajemen PemasaranPengantar Bisnis - 9 Manajemen Pemasaran
Pengantar Bisnis - 9 Manajemen Pemasaran
AndiNurkholis1
 
Technopreneurship - 6 Business Plan
Technopreneurship - 6 Business PlanTechnopreneurship - 6 Business Plan
Technopreneurship - 6 Business Plan
AndiNurkholis1
 
Pengantar Bisnis - 8 Kepemimpinan
Pengantar Bisnis - 8 KepemimpinanPengantar Bisnis - 8 Kepemimpinan
Pengantar Bisnis - 8 Kepemimpinan
AndiNurkholis1
 
Technopreneurship - 5 Model Bisnis
Technopreneurship - 5 Model BisnisTechnopreneurship - 5 Model Bisnis
Technopreneurship - 5 Model Bisnis
AndiNurkholis1
 
Technopreneurship - 4 Studi Kelayakan Usaha
Technopreneurship - 4 Studi Kelayakan UsahaTechnopreneurship - 4 Studi Kelayakan Usaha
Technopreneurship - 4 Studi Kelayakan Usaha
AndiNurkholis1
 
Pengantar Bisnis - 7 Motivasi Kerja
Pengantar Bisnis - 7 Motivasi KerjaPengantar Bisnis - 7 Motivasi Kerja
Pengantar Bisnis - 7 Motivasi Kerja
AndiNurkholis1
 
Pengantar Bisnis - 6 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 6 Manajemen Sumber Daya ManusiaPengantar Bisnis - 6 Manajemen Sumber Daya Manusia
Pengantar Bisnis - 6 Manajemen Sumber Daya Manusia
AndiNurkholis1
 
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian BisnisPengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
Pengantar Bisnis - 5 Pengelolaan & Pengorganisasian Bisnis
AndiNurkholis1
 
Technopreneurship - 3 Ide dan Prinsip Bisnis
Technopreneurship - 3 Ide dan Prinsip BisnisTechnopreneurship - 3 Ide dan Prinsip Bisnis
Technopreneurship - 3 Ide dan Prinsip Bisnis
AndiNurkholis1
 
Pengantar Bisnis - 4 Bentuk Organisasi Bisnis
Pengantar Bisnis - 4 Bentuk Organisasi BisnisPengantar Bisnis - 4 Bentuk Organisasi Bisnis
Pengantar Bisnis - 4 Bentuk Organisasi Bisnis
AndiNurkholis1
 
Technopreneurship - 2 Pengantar Technopreneurship
Technopreneurship - 2 Pengantar TechnopreneurshipTechnopreneurship - 2 Pengantar Technopreneurship
Technopreneurship - 2 Pengantar Technopreneurship
AndiNurkholis1
 
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis InternasionalPengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
Pengantar Bisnis - 3 Globalisasi Ekonomi & Bisnis Internasional
AndiNurkholis1
 
Pengantar Bisnis - 2 Bisnis dan Lingkungan Bisnis
Pengantar Bisnis - 2 Bisnis dan Lingkungan BisnisPengantar Bisnis - 2 Bisnis dan Lingkungan Bisnis
Pengantar Bisnis - 2 Bisnis dan Lingkungan Bisnis
AndiNurkholis1
 
Technopreneurship - 1 Kontrak Perkuliahan
Technopreneurship - 1 Kontrak PerkuliahanTechnopreneurship - 1 Kontrak Perkuliahan
Technopreneurship - 1 Kontrak Perkuliahan
AndiNurkholis1
 
Pengantar Bisnis - 1 Kontrak Perkuliahan
Pengantar Bisnis - 1 Kontrak PerkuliahanPengantar Bisnis - 1 Kontrak Perkuliahan
Pengantar Bisnis - 1 Kontrak Perkuliahan
AndiNurkholis1
 
Pemrograman Aplikasi Mobile - 11 Mobile Security
Pemrograman Aplikasi Mobile - 11 Mobile SecurityPemrograman Aplikasi Mobile - 11 Mobile Security
Pemrograman Aplikasi Mobile - 11 Mobile Security
AndiNurkholis1
 
Ad

Recently uploaded (20)

TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 

Object Oriented Programming - 7.2. Polymorphism

  • 1. Object Oriented Programming Andi Nurkholis, S.Kom., M.Kom. Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 April 23, 2020
  • 3. Inheritance in Java Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways. 3
  • 4. 4 Inheritance in Java (cont.) For example, think of a superclass called Animal that has a method called Sound(). Subclasses of Animals could be Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the cat meow and the dog bark, etc.):
  • 5. Example 5 class Animal { public void animalSound() { System.out.println("The animal makes a sound"); } } Now we can create Animal as Super Class and make the animalSound() method:
  • 6. Example (cont.) Then we can create Cat as Sub Class of Animal and make a new different animalSound() method: 6 class Cat extends Animal { public void animalSound() { System.out.println("The cat says: meow meow"); } }
  • 7. Example (cont.) 7 class Dog extends Animal { public void animalSound() { System.out.println("The dog says: bark bark"); } } Then we can create Dog as Sub Class of Animal and make a new different animalSound() method:
  • 8. Example (cont.) 8 class MyMainClass { public static void main(String[] args) { Animal myAnimal = new Animal(); Animal d1 = new Cat(); Animal c1 = new Dog(); myAnimal.animalSound(); //same function name d1.animalSound(); //same function name c1.animalSound(); //same function name } } Now we can create Cat and Dog objects and call the animalSound() method on both of them:
  • 9. Explanation 9 In previous slide, we have three same function names from three different classes that related each other. What the meaning? Polymorphism means same function name (but different signatures) being uses for different types.
  • 10. Types of Polymorphism In Java polymorphism is mainly divided into two types: • Run-time Polymorphism • Compile-time Polymorphism 10
  • 11. 11 Run-time Polymorphism In Java, run-time polymorphism can be achieved through method overriding. Suppose the same method is created in the superclass and its subclasses. In this case, the method that will be called depends upon the object used to call the method. Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
  • 12. Illustration In previous example (p. 5-8), that is an example of run-time polymorphism. Lets check the illustration: 12
  • 13. Explanation 13 In the previous illustration, the method makeSound() has different implementations in two different classes. When we run the program, • The expression d1.makeSound() will call the method of Dog class. It is because d1 is an object of the Dog class. • The expression c1.makeSound() will call the method of Cat class. It is because c1 is an object of the cat class.
  • 14. Compile-time Polymorphism The compile-time polymorphism can be achieved through method overloading and operator overloading in Java. Method Overloading, there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments. For example: 14 void func() { ... } void func(int a) { ... } float func(double a) { ... } float func(int a, float b) { ... }
  • 15. 15 Example class Demo { public void displayPattern(){ for(int i = 0; i < 10; i++) { System.out.print("*"); } } public void displayPattern(char symbol) { for(int i = 0; i < 10; i++) { System.out.print(symbol); } } }
  • 16. 16 Example (cont.) class Main { public static void main(String[] args) { Demo d1 = new Demo(); d1.displayPattern(); System.out.println("n"); d1.displayPattern('#'); } } //output ********** ##########
  • 17. Explanation In previous example, the displayPattern() method is overloaded. • If we call the method without passing any arguments, a pattern of * is created. • If we call the method by passing a character as an argument, a pattern of that character is created. 17
  • 18. Overloading Vs Overriding • In the case of method overriding, methods should be inside different classes. Whereas, in the case of method overloading, methods should be inside the same class. • Method overriding is performed at run-time whereas method overloading is performed at compile-time. 18
  • 19. So, Why “Polymorphism"? Polymorphism allows us to create consistent code. For example, Suppose we need to render a circle and a square. To do so, we can create a Polygon class and inherited two subclasses Circle and Square from it. In this case, it makes sense to create a method having the same name render() in both these subclasses rather than creating methods with different names. In previous method overloading example, we have used the same method name displayPattern() to display two different patterns for consistency. 19
  • 20. Thank You, Next … Encapsulation Study Program of Informatics Faculty of Engineering and Computer Science SY. 2019-2020 Andi Nurkholis, S.Kom., M.Kom. April 23, 2020