SlideShare ist ein Scribd-Unternehmen logo
1 von 19
CMP-2123-Object Oriented
Programming
Lecture 8
By
Abrar Ahmad
Abrar.ahmad14@ce.ceme.edu.pk
Java – Polymorphism
Badar Waseer arbabwaseergmail.com 2
• Polymorphism is the ability of an object to take on many forms. The
most common use of polymorphism in OOP occurs when a parent
class reference is used to refer to a child class object.
• Any Java object that can pass more than one IS-A test is considered to
be polymorphic. In Java, all Java objects are polymorphic since any
object will pass the IS-A test for their own type and for the class
Object.
• It is important to know that the only possible way to access an object
is through a reference variable. A reference variable can be of only
one type. Once declared, the type of a reference variable cannot be
changed
Badar Waseer arbabwaseergmail.com 3
• The reference variable can be reassigned to other objects provided
that it is not declared final. The type of the reference variable would
determine the methods that it can invoke on the object.
• A reference variable can refer to any object of its declared type or any
subtype of its declared type. A reference variable can be declared as a
class or interface type.
Badar Waseer arbabwaseergmail.com 4
• Let us look at an example.
Badar Waseer arbabwaseergmail.com 5
• Now, the Deer class is considered to be polymorphic since this has
multiple inheritance. Following are true for the above examples −
• A Deer IS-A Animal
• A Deer IS-A Vegetarian
• A Deer IS-A Deer
• A Deer IS-A Object
Badar Waseer arbabwaseergmail.com 6
• When we apply the reference variable facts to a Deer object
reference, the following declarations are legal −
• All the reference variables d, a, v, o refer to the same Deer object in
the heap.
Badar Waseer arbabwaseergmail.com 7
Virtual Methods
• In this section, I will show you how the behavior of overridden
methods in Java allows you to take advantage of polymorphism when
designing your classes.
• We already have discussed method overriding, where a child class can
override a method in its parent. An overridden method is essentially
hidden in the parent class, and is not invoked unless the child class
uses the super keyword within the overriding method.
Badar Waseer arbabwaseergmail.com 8
Badar Waseer arbabwaseergmail.com 9
• Now suppose we extend Employee class as follows −
Badar Waseer arbabwaseergmail.com 10
Badar Waseer arbabwaseergmail.com 11
• Now, you study the following program carefully and try to determine
its output −
Badar Waseer arbabwaseergmail.com 12
Badar Waseer arbabwaseergmail.com 13
• This will produce the following result −
Badar Waseer arbabwaseergmail.com 14
Badar Waseer arbabwaseergmail.com 15
• Here, we instantiate two Salary objects. One using a Salary
reference s, and the other using an Employee reference e.
• While invoking s.mailCheck(), the compiler sees mailCheck() in the
Salary class at compile time, and the JVM invokes mailCheck() in the
Salary class at run time.
• mailCheck() on e is quite different because e is an Employee
reference. When the compiler sees e.mailCheck(), the compiler sees
the mailCheck() method in the Employee class.
Badar Waseer arbabwaseergmail.com 16
• Here, at compile time, the compiler used mailCheck() in Employee to
validate this statement. At run time, however, the JVM invokes
mailCheck() in the Salary class.
• This behavior is referred to as virtual method invocation, and these
methods are referred to as virtual methods. An overridden method is
invoked at run time, no matter what data type the reference is that
was used in the source code at compile time.
Badar Waseer arbabwaseergmail.com 17
Any Questions ?
Badar Waseer arbabwaseergmail.com 18
Thank You
Badar Waseer arbabwaseergmail.com 19

Weitere ähnliche Inhalte

Was ist angesagt? (7)

Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Topic inheritance
Topic  inheritanceTopic  inheritance
Topic inheritance
 
Java keywords
Java keywordsJava keywords
Java keywords
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
 
CPP05 - Arrays
CPP05 - ArraysCPP05 - Arrays
CPP05 - Arrays
 
Semantic DEX Components
Semantic DEX ComponentsSemantic DEX Components
Semantic DEX Components
 
Lightning Talk on Metaprogramming in Ruby
Lightning Talk on Metaprogramming in RubyLightning Talk on Metaprogramming in Ruby
Lightning Talk on Metaprogramming in Ruby
 

Ähnlich wie Lec 1.8 Object Oriented Programming

Inheritance
InheritanceInheritance
Inheritance
hccit
 

Ähnlich wie Lec 1.8 Object Oriented Programming (20)

Chapter08 - Inheritance.ppt
Chapter08 - Inheritance.pptChapter08 - Inheritance.ppt
Chapter08 - Inheritance.ppt
 
Top 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed AnswersTop 100 Java Interview Questions with Detailed Answers
Top 100 Java Interview Questions with Detailed Answers
 
Inheritance
InheritanceInheritance
Inheritance
 
03- OOP Concepts.pdf
03- OOP Concepts.pdf03- OOP Concepts.pdf
03- OOP Concepts.pdf
 
Java session2
Java session2Java session2
Java session2
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
Java
JavaJava
Java
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
OOPS Characteristics
OOPS CharacteristicsOOPS Characteristics
OOPS Characteristics
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation A Case Study on Java. Java Presentation
A Case Study on Java. Java Presentation
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 

Mehr von Badar Waseer

Mehr von Badar Waseer (15)

Ch 03 s.e agile development
Ch 03 s.e agile developmentCh 03 s.e agile development
Ch 03 s.e agile development
 
Ch 02 s.e software process models 1
Ch 02 s.e software process models   1Ch 02 s.e software process models   1
Ch 02 s.e software process models 1
 
Ch 01 s.e introduction
Ch 01 s.e introductionCh 01 s.e introduction
Ch 01 s.e introduction
 
Ch 14 s.e use case diagrams
Ch 14 s.e use case diagramsCh 14 s.e use case diagrams
Ch 14 s.e use case diagrams
 
Ch 13 s.e cmmi
Ch 13 s.e cmmiCh 13 s.e cmmi
Ch 13 s.e cmmi
 
Lec 1.1 Object Oriented Programming
Lec 1.1 Object Oriented ProgrammingLec 1.1 Object Oriented Programming
Lec 1.1 Object Oriented Programming
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented Programming
 
Multimedia System & Design Ch 7 multimedia skills
Multimedia System & Design Ch 7 multimedia skillsMultimedia System & Design Ch 7 multimedia skills
Multimedia System & Design Ch 7 multimedia skills
 
Multimedia System & Design Ch 6 animation
Multimedia System & Design Ch 6 animationMultimedia System & Design Ch 6 animation
Multimedia System & Design Ch 6 animation
 
Multimedia System & Design Ch 5 video
Multimedia System & Design Ch 5 videoMultimedia System & Design Ch 5 video
Multimedia System & Design Ch 5 video
 
Multimedia System & Design Ch 4 Audio
Multimedia System & Design Ch 4 AudioMultimedia System & Design Ch 4 Audio
Multimedia System & Design Ch 4 Audio
 
Multimedia System & Design Ch 1, 2, 3 Multimedia
Multimedia System & Design Ch 1, 2, 3 MultimediaMultimedia System & Design Ch 1, 2, 3 Multimedia
Multimedia System & Design Ch 1, 2, 3 Multimedia
 
Multimedia System & Design Ch 8 delivering
Multimedia System & Design Ch 8 deliveringMultimedia System & Design Ch 8 delivering
Multimedia System & Design Ch 8 delivering
 
Agriculture Information System (AIS)
Agriculture Information System (AIS)Agriculture Information System (AIS)
Agriculture Information System (AIS)
 
Output Devices UoS Fsd
Output Devices UoS FsdOutput Devices UoS Fsd
Output Devices UoS Fsd
 

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Kürzlich hochgeladen (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 

Lec 1.8 Object Oriented Programming

  • 1. CMP-2123-Object Oriented Programming Lecture 8 By Abrar Ahmad Abrar.ahmad14@ce.ceme.edu.pk
  • 2. Java – Polymorphism Badar Waseer arbabwaseergmail.com 2
  • 3. • Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. • Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object. • It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed Badar Waseer arbabwaseergmail.com 3
  • 4. • The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object. • A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type. Badar Waseer arbabwaseergmail.com 4
  • 5. • Let us look at an example. Badar Waseer arbabwaseergmail.com 5
  • 6. • Now, the Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for the above examples − • A Deer IS-A Animal • A Deer IS-A Vegetarian • A Deer IS-A Deer • A Deer IS-A Object Badar Waseer arbabwaseergmail.com 6
  • 7. • When we apply the reference variable facts to a Deer object reference, the following declarations are legal − • All the reference variables d, a, v, o refer to the same Deer object in the heap. Badar Waseer arbabwaseergmail.com 7
  • 8. Virtual Methods • In this section, I will show you how the behavior of overridden methods in Java allows you to take advantage of polymorphism when designing your classes. • We already have discussed method overriding, where a child class can override a method in its parent. An overridden method is essentially hidden in the parent class, and is not invoked unless the child class uses the super keyword within the overriding method. Badar Waseer arbabwaseergmail.com 8
  • 10. • Now suppose we extend Employee class as follows − Badar Waseer arbabwaseergmail.com 10
  • 12. • Now, you study the following program carefully and try to determine its output − Badar Waseer arbabwaseergmail.com 12
  • 14. • This will produce the following result − Badar Waseer arbabwaseergmail.com 14
  • 16. • Here, we instantiate two Salary objects. One using a Salary reference s, and the other using an Employee reference e. • While invoking s.mailCheck(), the compiler sees mailCheck() in the Salary class at compile time, and the JVM invokes mailCheck() in the Salary class at run time. • mailCheck() on e is quite different because e is an Employee reference. When the compiler sees e.mailCheck(), the compiler sees the mailCheck() method in the Employee class. Badar Waseer arbabwaseergmail.com 16
  • 17. • Here, at compile time, the compiler used mailCheck() in Employee to validate this statement. At run time, however, the JVM invokes mailCheck() in the Salary class. • This behavior is referred to as virtual method invocation, and these methods are referred to as virtual methods. An overridden method is invoked at run time, no matter what data type the reference is that was used in the source code at compile time. Badar Waseer arbabwaseergmail.com 17
  • 18. Any Questions ? Badar Waseer arbabwaseergmail.com 18
  • 19. Thank You Badar Waseer arbabwaseergmail.com 19