Polymorphism CSCE 121 J. Michael Moore.

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Chapter 15 Polymorphism and Virtual Functions. Learning Objectives Virtual Function Basics – Late binding – Implementing virtual functions – When to use.
Day Virtual versus static methods. 2. Polymorphism 3. Detailed worked example. 4. Abstract classes.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.
Polymorphism Polymorphism occurs with objects instantiated from a set of classes related by inheritance to the same ancestor class. –Each class provides.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
CMSC202 Computer Science II for Majors Lecture 14 – Polymorphism Dr. Katherine Gibson.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Inheritance and Big O And your first reading assignment
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Inheritance and Run time Polymorphism
Phil Tayco Slide version 1.0 Created Nov 6, 2017
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Types of Programming Languages
Comp 249 Programming Methodology
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Inheritance Basics Programming with Inheritance
Chapter 9: Polymorphism and Inheritance
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Inheritance, Polymorphism, and Virtual Functions
Polymorphism Phil Tayco San Jose City College Slide version 1.1
Anatomy of Polymorphism
Polymorphism, Abstract Classes & Interfaces
Polymorphism Polymorphism
Computer Programming with JAVA
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Inheritance and Polymorphism
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Function Overloading CSCE 121 J. Michael Moore
Polymorphism.
Inheritance in Graphics
CISC/CMPE320 - Prof. McLeod
Polymorphism & Pointers
Fundaments of Game Design
Anatomy of Polymorphism
Polymorphism & Pointers
Review of Previous Lesson
Object-Oriented PHP (1)
Lecture 0311 – Polymorphism
COP 3330 Object-oriented Programming in C++
Topics OOP Review Inheritance Review Abstract Classes
C++ Polymorphism Reference and pointer implicit type casting
C++ Object Oriented 1.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

Polymorphism CSCE 121 J. Michael Moore

Recall When you create an instance of a derived class, it is both: an instance of the base class an instance of the derived class. So all of the actions/attributes possible with the parent class are possible with the derived class.

Results differ based on the type of animal. Speak! Same command! Results differ based on the type of animal.

Polymorphism Basic A little Greek What does poly mean? What does morph mean? So when the person says “Speak”, each animal (morph / shape) speaks according to what type of animal it is. With polymorphism, we can indicate a method (e.g. speak) and regardless of the underlying type, it will do what is appropriate for that type (e.g. “quack”, “meow”, “woof”).

Person Example Person (parent / base class) Firefighter (child / derived class) Doctor (child / derived class) Chef (child / derived class)

C++ Can only see an object as one thing at a time. E.g. can only treat an object as a person or as a firefighter depending on the current view (datatype… more in anatomy) Suppose you create a method/attribute in a derived class that exactly matches something in the parent (note: this is allowed) If looking through firefighter lens, the firefighter version is used. If looking through person lens, the person version is used.

Simple Interface We only need to know one method to use with an unknown number of sub-classes. So, with polymorphism, we can use the person lens to look at people and we do not have to worry about what sub-class each one is. However, not very useful if we can only do what the parent class does by default.

Polymorphism Useful What if we want to use the sub-class version instead of the parent-class version? E.g. the firefighter version instead of the person version There needs to be a way to indicate that the sub-class version should override the parent version.

Virtual Indicate in the parent that the child version should be used instead if parent version is also available. In C++ this is known as a virtual function. When looking at an instance of a base class, when it sees “virtual” it knows to use the derived class version of the function. If the derived class does not have its own version, it will use the base class version.

Abstract Suppose there is not a meaningful version to use in the base class? Indicate that the method is abstract. In C++ this is called a pure virtual function. When looking at an instance of a base class, when it sees “pure virtual” it knows it looks for the derived version. If the derived class does not have its own version, it will not compile.

Abstract Class An abstract class cannot be instantiated! In C++ this is accomplished by having at least one pure virtual function in the base class. This makes sense. If you have a pure virtual function, there is no default definition for that function, so it cannot be created. Child classes that will be instantiated MUST define its version of the pure virtual function.

Guidelines Make it a virtual Make it pure virtual Want derived classes to have the option of overriding a function since a default is provided. Make it a virtual Want to force derived classes to override a function (i.e. do not provide a default) Make it pure virtual

UML Update for Virtual UML supports indicating polymorphism by indicating virtual functions. Virtual functions are written in italics. If hand written, underline. There is not a way to indicate pure virtual functions in standard UML. On exams I will ask you to put “pv” before functions that should be pure virtual.

Different ways to drive. Vehicle -weight -length -height -width Flintstone +drive() +stop() Front Wheel Drive +drive() +stop() +drive() +stop() Virtual functions Flintstone image taken from: http://www.dailyrecord.co.uk/news/local-news/yabba-dabba-shotts-man-puts-7654559#sDmfmSD8V1mpZXUH.97 FWD image: By Moebiusuibeom-en (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0) or GFDL (http://www.gnu.org/copyleft/fdl.html)], via Wikimedia Commons