Polymorphism.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Polymorphism Method overriding Method overloading Dynamic binding 1.
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.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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 and Polymorphism Daniel Liang, Introduction to Java Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Inheritance and Polymorphism
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
BY:- TOPS Technologies
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
EKT472: Object Oriented Programming Overloading and Overriding Method.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Design issues for Object-Oriented Languages
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Java Unit 11: Inheritance I
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Advanced Programming in Java
Overriding Method.
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Object Oriented Programming
Designing for Inheritance
Chapter 9 Inheritance and Polymorphism
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
Chapter 9: Polymorphism and Inheritance
Polymorphism CT1513.
Polymorphism, Abstract Classes & Interfaces
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Method Overriding in Java
Sampath Kumar S Assistant Professor, SECE
Polymorphism CT1513.
Object Oriented Programming (OOP) LAB # 9
Java Inheritance.
Polymorphism.
Chapter 11 Inheritance and Polymorphism Part 1
Advanced Programming in Java
Programming in C# CHAPTER 5 & 6
CPSC 233 Tutorial 13 March 11/12th, 2015.
Presentation transcript:

Polymorphism

Agenda What is & Why Polymorphism? Polymorphism Examples. Dynamic binding.

What is & Why Polymorphism?

What is Polymorphism Generally, polymorphism refers to the ability to appear in many forms. In java Allows multiple objects of different subclasses to be treated as objects of a single super class, while automatically selecting the proper methods to apply to a particular object based on the subclass it belongs to

Polymorphism Example For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results.

Polymorphism Examples

Example 1: Polymorphism When parent class reference is used to refer to a child class object. Given the parent class Person and the child class Student, we add another subclass of Person which is Employee. Below is the class hierarchy

Example 1: Polymorphism Now suppose we have a printName method in our super class Person, and we override this method in both Student and Employee subclass's. public class Student extends Person { public void printName(){ System.out.println("Student Name" ); }} public class Employee extends Person { System.out.println("Employee Name" );

Example 1: Polymorphism In Java, we can create a reference that is of type super class, Person, to an object of its subclass, Student. public static main( String[] args ) { Student studentObject = new Student(); Employee employeeObject = new Employee(); // Person reference point to a Student object Person ref = studentObject; // Calling printName() of the Student object instance ref.printName(); }

Example 1: Polymorphism Going back to our main method, when we try to call the printName method of the reference Person ref, the printName method of the Student object will be called. Now, if we assign ref to an Employee object, the printName method of Employee will be called.

Example 2: Polymorphism Another example that illustrates polymorphism is when we try to pass a reference to methods as a parameter. Suppose we have a static method printlnformation that takes in a Person reference as parameter. public static void printInformation( Person p ){ // It will call printName() method of the // actual object instance that is passed p.printName(); }

Example 2: Polymorphism We can actually pass a reference of type Employee and type Student to the printInformation method as long as it is a subclass of the Person class. public static main( String[] args ) { Student studentObject = new Student(); Employee employeeObject = new Employee(); printInformation( studentObject ); printInformation( employeeObject ); Output: Student Name Employee Name

Dynamic binding

Static and dynamic binding Static binding: Which Method will be invocated will be determined at compile time. Dynamic binding: Which Method will be invocated will be determined at runtime time.

Static and dynamic binding What happen when I make thing like this? Animal a=new Horse(); a.eat();//will call which one a.buck();

Static and dynamic binding Note : Method invocations allowed by the compiler are based solely on the declared type of the reference, regardless of the object type Which overridden version of the method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type

Static and dynamic binding Compiler error

Questions

Thanks