Polymorphism CMPS 2143. Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.

Slides:



Advertisements
Similar presentations
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Advertisements

Object Oriented Programming with Java
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.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Chapter 1 Inheritance University Of Ha’il.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
ReviewSlides CMPS Advantages/Disadvantages Statically typed languages ▫ Type checking can be done at compile time ▫ Memory layout can be determined.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Advanced Object-Oriented Programming Features
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
C++ Training Datascope Lawrence D’Antonio Lecture 5 An Overview of C++: What is Polymorphism? - Overloading.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
OOP Languages: Java vs C++
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Programming Languages and Paradigms Object-Oriented Programming.
CSCI-383 Object-Oriented Programming & Design Lecture 23.
Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.
1 Using Templates COSC 1567 C++ Programming Lecture 10.
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
Lecture 5 : Inheritance & Polymorphism Acknowledgement : courtesy of Prof. Timothy Budd lecture slides.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Chapter More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
UMBC CMSC 331 Java Review of objects and variables in Java.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,
CSCI 383 Object-Oriented Programming & Design Lecture 21 Martin van Bommel.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
CS212: Object Oriented Analysis and Design
Object Oriented Analysis and Design
OOP’S Concepts in C#.Net
Inheritance Basics Programming with Inheritance
CSC 113: Computer programming II
Computer Programming with JAVA
Sampath Kumar S Assistant Professor, SECE
CS2011 Introduction to Programming I Methods (II)
Chapter 10: Method Overriding and method Overloading
Inheritance and Polymorphism
Method Overriding and method Overloading
Eighth step for Learning C++ Programming
C++ Programming CLASS This pointer Static Class Friend Class
Inheritance and Polymorphism
Presentation transcript:

Polymorphism CMPS 2143

Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional language users and OOP language users Means there is ONE name, many different meanings ▫ Names: variables, functions, methods, class names ▫ Meanings: can be defined in different ways 2

Four forms of polymorphism Overloading Overriding Polymorphic variables Generics (templates) 3

Overloading – More in Chapter 15 AKA ad-hoc polymorphism Single function/method name has several alternative implementations Distinguished at compile time by type signature class myClass { public: //3 overloaded meanings for same name void example (int x) {…} void example (int x, double y) {…} void example (string x) {…} } 4

Overriding – More in Chapter 16 Single function/method name has several alternative implementations with the same signature, but occurs within context of the parent class/child class relationship class Parent { public: void example (int x) {…} } class Child { public: //same name, different method body void example (int x) {…} } 5

Polymorphic Variable – More in Chapter 17 AKA assignment polymorphism Variable is declared of one type, but holds a value of a different type //declared as parent, holding child value Room r = new King(…); When a polymorphic variable is used as an argument, the result function/method is said to exhibit pure polymorphism. 6

Generics/Templates – More in Chapter 18 A way of creating general purpose tools and specializing them to specific situations template T max (T left, T right){ if (left < right) return right; return left; } A generic function or class is parameterized by type ▫ Similar to the way a function is parameterized by values The type is unspecified, to be filled in later 7

Many tools, one goal Polymorphism is the feature of OOP languages that distinguish them the most from other types of languages Each different polymorphic technique permits a different form of reuse 8

Study questions Pg. 286: 2-5, 9 9