10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.

Slides:



Advertisements
Similar presentations
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
OVERRIDING/OVERLOADING Srinivas. EXAM OBJECTIVES  Given a code example, determine if a method is correctly overriding or overloading another method,
A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
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.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 10 Inheritance and Polymorphism
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
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 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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Polymorphism Lecture - 9.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
BY:- TOPS Technologies
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
EKT472: Object Oriented Programming Overloading and Overriding Method.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Polymorphism.
Advanced Programming in Java
Overriding Method.
Chapter 11 Inheritance and Polymorphism
CompSci 230 S Programming Techniques
ATS Application Programming: Java Programming
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
Advanced Java Topics Chapter 9
CMSC 202 Polymorphism.
METHOD OVERRIDING in JAVA
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Method Overriding in Java
Polymorphism CT1513.
Polymorphism Polymorphism - Greek for “many forms”
Polymorphism.
Chapter 9 Carrano Chapter 10 Small Java
Sampath Kumar S Assistant Professor, SECE
Chapter 11 Inheritance and Polymorphism Part 1
Advanced Programming in Java
Presentation transcript:

10 Polymorphism

2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism

3 Objectives Explain the concept of polymorphism Describe and distinguish method overloading Describe and distinguish method overriding Explain the concepts of early binding and late binding Demonstrate when it is appropriate to implement polymorphism

4 Defining Polymorphism Polymorphism is the ability of different objects to respond to the same message in different ways. This means that different objects can have very different method implementations for the same message. Polymorphism is the ability of a new object to implement the base functionality of a parent object in a new way. Polymorphism is an object's ability to behave differently depending on its type. Polymorphism is the ability of objects belonging to different types to respond to methods of the same name, each one according to the appropriate type-specific behavior.

5 Method Overloading Method Overloading is the process of declaring methods with the same name but different parameter types. A method can be overloaded in the same class or in a subclass. Which overloaded method to call is based on reference type and decided at compile time. Which overridden method to call is based on object type and decided during runtime.

6 Rules of Method Overloading 1.Overloaded methods must change the argument list. 2.Overloaded methods can change the return type. 3.Overloaded methods can change the access modifier. 4.Overloaded methods can declare new or broader checked exceptions.

7 Implementing Method Overloading public static void main(String[] args) { Sales s = new Sales(); System.out.println(s.computeSales(100)); System.out.println(s.computeSales(100,2)); System.out.println(s.computeSales(100,2,30)); } class Sales { double computeSales(double price) { double sales; sales = price; return sales; } double computeSales(double price, int qty) { double sales; sales = price * qty; return sales; } double computeSales(double price, int qty, double discount) { double sales; sales = (price * qty) - discount; return sales; } }

8 Method Overriding Method Overriding allows a subclass to redefine methods of the same name from the superclass. The key benefit of overriding is the ability to define/defer behavior specific to subclasses. Which overridden method to call is based on object type and decided at runtime.

9 Rules of Method Overriding 1.An overridden method must have the same name the same number of parameters and types the same return type as the overridden method. 2.Overriding a method cannot narrow the method access level defined in the overridden method. 3.Overriding a method cannot widen checked exceptions defined in the overridden method. 4.Methods declared as private, static, or final cannot be overridden. 5.A static method cannot override an instance method.

10 Implementing Method Overriding public static void main(String[] args) { Sales s = new Sales(); Sales st = new SalesTax(); System.out.println(s.computeSales(100)); System.out.println(s.computeSales(100,2)); System.out.println(s.computeSales(100,2,30)); System.out.println(st.computeSales(100)); } class Sales { double computeSales(double price) { double sales = price; return sales; } double computeSales(double price, int qty) { double sales = price * qty; return sales; } double computeSales(double price, int qty, double discount) { double sales = (price * qty) - discount; return sales; } } class SalesTax extends Sales { double computeSales(double price) { double sales = price * 1.10; // add tax return sales; } } Define Sales reference variable containing SalesTax object It determined which method to run based on object type (SalesTax) instead of reference type (Sales)

11 Overloading vs Overriding CriteriaOverloaded MethodOverridden Method Argument listDifferentSame Return typeCan changeSame ExceptionsCan changeCannot be wider Access levelCan changeCannot be narrower InvocationBased on reference type and decided at compile time Based on object type and decided at runtime

12 Early Binding and Late Binding Early binding means translating operations or associating identifiers during compile time. Late binding means delaying translation of an operation or associating identifiers at runtime (also known as dynamic method lookup or virtual method invocation ). It is used in polymorphism to determine the actual method invoked (depending on the type of the actual object).

13 Implementing Polymorphism class Animal { public void eat() { System.out.println("Animal eating...");} } class Snake extends Animal { public void eat() { System.out.println("Snake eating...");} } class Horse extends Animal { public void eat() { System.out.println("Horse eating...");} public void eat(String s) { System.out.println("Horse eating " + s); } } public static void main(String[] args) { Animal pig = new Animal(); Snake viper = new Snake(); Horse stallion = new Horse(); Animal animalViper = new Snake(); Animal animalStallion = new Horse(); pig.eat();// will this compile? Output? pig.eat("left overs");//will this compile? Output? viper.eat(); // will this compile? Output? viper.eat("chicken"); //will this compile? Output? stallion.eat(); // will this compile? Output? stallion.eat("grass"); // will this compile? Output? animalViper.eat();// will this compile? Output? animalViper.eat("rat"); //will this compile? Output? animalStallion.eat(); // will this compile? Output? animalStallion.eat("Carrots"); //will this compile? Output? } Animal eating... Snake eating... Horse eating... Horse eating grass Snake eating... Horse eating...

14 Key Points Polymorphism is an object's ability to behave differently depending on its type Method Overloading is the process of declaring methods with the same name but different parameter types Method Overriding allows a subclass to redefine methods of the same name from a superclass Early binding translates identifiers at compile-time while late binding translates identifiers during runtime Late binding is used in polymorphism to determine the actual method invoked at runtime