Polymorphism Polymorphism - Greek for “many forms”

Slides:



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

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.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
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.
Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 10: Inheritance and Polymorphism
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 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.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
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.
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
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.
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Polymorphism November 27, 2006 ComS 207: Programming I (in Java)
Advanced Java Topics Chapter 9
Advanced Programming in Java
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
ATS Application Programming: Java Programming
Object Oriented Programming
Advanced Java Topics Chapter 9 (continued)
Chapter 17 Linked Lists.
Chapter 4 Inheritance.
Figure 8.1 Inheritance: Relationships among timepieces.
Advanced Java Topics Chapter 9
Polymorphism CT1513.
CMSC 202 Polymorphism.
Java – Inheritance.
Polymorphism CT1513.
Implementing Subprograms
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
CIS 199 Final Review.
Chapter 6 Dynamic Programming.
Chapter 2 Reference Types.
Advanced Programming in Java
Chapter 7 Inheritance.
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

Polymorphism Polymorphism - Greek for “many forms” A superclass that using a superclass variable to reference a subclass method. Example: Shape s = new Circle(); s.getArea(); © 2006 Pearson Addison-Wesley. All rights reserved

Dynamic Binding A polymorphic method Late binding or dynamic binding A method that has multiple meanings Created when a subclass overrides a method of the superclass Late binding or dynamic binding The appropriate version of a polymorphic method is decided at execution time © 2006 Pearson Addison-Wesley. All rights reserved

Dynamic Binding Figure 9-7a area is overridden: a) mySphere.DisplayStatistics( ) calls area in Sphere © 2006 Pearson Addison-Wesley. All rights reserved

Dynamic Binding Figure 9-7b area is overridden: b) myBall.displayStatistics( ) calls area in Ball © 2006 Pearson Addison-Wesley. All rights reserved

Dynamic Binding Controlling whether a subclass can override a superclass method Field modifier final Prevents a method from being overridden by a subclass public final boolean writeDatabaseRecord() { … } Field modifier abstract Requires the subclass to override the method public abstract int getCount(); Early binding or static binding The appropriate version of a method is decided at compilation time Used by methods that are final or static © 2006 Pearson Addison-Wesley. All rights reserved

Dynamic Binding and Abstract Classes Overloading methods To overload a method is to define another method with the same name but with a different set of parameters The arguments in each version of an overloaded method determine which version of the method will be used Example: public void printDetails(Person p) { … } public void printDetails(Elephant e) { … } public void printDetails(Car c) { … } © 2006 Pearson Addison-Wesley. All rights reserved

Things to know Difference between overloading and overriding How polymorphism is implemented and what version of the method gets called. Difference between static binding and dynamic/late binding What a “final” method means What an “abstract” method means © 2006 Pearson Addison-Wesley. All rights reserved