Sampath Kumar S Assistant Professor, SECE

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Programming With Java ICS Chapter 8 Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.
Bayu Priyambadha, S.Kom. * Poly = Many, Morph = form * Polymorphism refers to a principle in biology in which an organism or species can have many different.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
Chapter -6 Polymorphism
Peyman Dodangeh Sharif University of Technology Fall 2014.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
UMBC CMSC 331 Java Review of objects and variables in Java.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
BY:- TOPS Technologies
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
EKT472: Object Oriented Programming Overloading and Overriding Method.
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
Polymorphism.
Inheritance ITI1121 Nour El Kadri.
Advanced Programming in Java
Lecture 12 Inheritance.
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Lecture 17: Polymorphism (Part II)
INHERITANCE IN JAVA.
Continuing Chapter 11 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
OOP’S Concepts in C#.Net
Chapter 9 Inheritance and Polymorphism
תוכנה 1 תרגול מספר 11: Static vs. Dynamic Binding
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Sampath Kumar S Assistant Professor, SECE
CMSC 202 Polymorphism.
Sampath Kumar S Assistant Professor, SECE
Sampath Kumar S Assistant Professor, SECE
Sampath Kumar.S Assistant Professor/IT, SECE
Method Overloading in JAVA
Sampath Kumar S Assistant Professor, SECE
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Method Overriding in Java
Polymorphism CT1513.
Graphics Programming - Frames
Sampath Kumar S Assistant Professor, SECE
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Polymorphism.
Lecture 18: Polymorphism (Part II)
Sampath Kumar S Assistant Professor, SECE
Sampath Kumar S Assistant Professor, SECE
Advanced Programming in Java
Computer Science II for Majors
Presentation transcript:

Sampath Kumar S Assistant Professor, SECE Polymorphism Sampath Kumar S Assistant Professor, SECE

Polymorphism 1/12/2019 It is a mechanism which allows to have many forms of the method having the same name. (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. Sampath Kumar S, AP

Types of polymorphism in java 1/12/2019 There are two types of polymorphism in Java: Runtime polymorphism (Dynamic polymorphism) Compile time polymorphism (Static polymorphism). Sampath Kumar S, AP

Runtime Polymorphism (Dynamic polymorphism) 1/12/2019 Method overriding is a perfect example of  runtime polymorphism. In this kind of polymorphism, reference of class X can hold object of class X or an object of any sub classes of class X. For e.g. if class Y extends class X then both of the following statements are valid: Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y(); Sampath Kumar S, AP

1/12/2019 Cont.., Since in method overriding both the classes(base class and child class) have same method, compile doesn’t figure out which method to call at compile-time. In this case JVM (Java Virtual Machine) decides which method to call at runtime that’s why it is known as runtime or dynamic polymorphism. Sampath Kumar S, AP

Example 1/12/2019 public class A { public void methodA() { //Base class method System.out.println("methodA of class A"); } } public class B extends A { public void methodA() { //Derived Class method System.out.println ("methodA of class B"); public class Z { public static void main (String args []) { A obj1 = new A(); // Reference and object B A obj2 = new B(); // A reference but B object obj1.methodA(); obj2.methodA(); } Note: As you can see the methodA has different-2 forms in child and parent class thus we can say methodA here is polymorphic. Output: methodA of class A methodA of class B Sampath Kumar S, AP

Compile time Polymorphism (Static polymorphism) 1/12/2019 Compile time Polymorphism (Static polymorphism) Compile time polymorphism is nothing but the method overloading in java. In simple terms we can say that a class can have more than one methods with same name but with different number of arguments or different types of arguments or both. Sampath Kumar S, AP

Example Output: methodA: 20 methodA: 20, 30 1/12/2019 Example class X { void methodA(int num) { System.out.println ("methodA: " + num); } void methodA(int n1, int n2) { System.out.println ("methodA: "+ n1 +“, "+ n2); public class Y { public static void main (String args []) { X Obj = new X(); double result; Obj. methodA(20); Obj. methodA(20, 30); Note: The class has 2 variance of methodA or we can say methodA is polymorphic in nature since it is having 2 different forms. In such scenario, compiler is able to figure out the method call at compile-time that’s the reason it is known as compile time polymorphism. Output: methodA: 20 methodA: 20, 30 Sampath Kumar S, AP

1/12/2019 Sampath Kumar S, AP

1/12/2019 Thank You  Sampath Kumar S, AP