Day 5. 1. Virtual versus static methods. 2. Polymorphism 3. Detailed worked example. 4. Abstract classes.

Slides:



Advertisements
Similar presentations
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function.
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.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
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.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter -6 Polymorphism
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance ndex.html ndex.htmland “Java.
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.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Overriding Method.
Inheritance and Polymorphism
Class A { public : Int x; A()
Polymorphism.
Inheritance and Run time Polymorphism
Object Oriented Programming
Designing for Inheritance
Inheritance Basics Programming with Inheritance
Java Programming Language
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Polymorphism Polymorphism
Computer Programming with JAVA
Java – Inheritance.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
COP 3330 Object-oriented Programming in C++
Chapter 11 Class Inheritance
C++ Object Oriented 1.
Lecture 6: Polymorphism
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.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Day Virtual versus static methods. 2. Polymorphism 3. Detailed worked example. 4. Abstract classes.

1. Virtual versus static methods. §A. Motivation. §Member methods can be either “static” or “virtual.” §What does this mean? §Intuitively, something is static if it it is fixed / cannot change, whereas a virtual feature can simulate a variety of different things.

Virtual versus static methods. §Analogy: §On a boat or in an airplane (and in some classrooms) the seats are bolted to the floor. Seating is “static.” §In some cars (and in many classrooms) the same seats can adopt many different configurations— many different virtual passenger areas / classrooms even though there is only one physical car / classroom.

Virtual versus static methods. §Unless it is upgraded, the RAM of a given machine is fixed. However, there can be a variable amount of “virtual memory” depending on demand, free drive space, etc. §By analogy, a member method is static, if and only if it is “fixed” to one class. This is the only class the method can belong to.

Virtual versus static methods. §But a member method is virtual if it can belong to a variety of classes, and the specific class is either determined by the compiler or not resolved until run-time.

Virtual versus static methods. §More technically, static member methods always use “early binding,” meaning that the method name (identifier) is bound to a particular class at compile time. §Virtual methods *may* use either early binding or late binding, meaning that the method name is bound to one or more methods at run time.

Why use virtual methods? §Suppose you have many similar classes e.g. within the Employ Hierarchy: §Hourly, Salaried, Contract, Temporary, Freelance, Daylight-robber, etc. §All of these classes may need similar methods e.g. they may all need Calculate and Print. §It would be nice if we could reuse the same method names throughout the hierarchy.

Virtual versus static methods. §To implement a virtual method: §(1) The method must be declared as virtual in the base class; §(2) A variety of versions of the method can be defined in the implementation files for the descendant classes using the keyword override. §The result is each version of the method has its own address.

Virtual versus static methods. §These addresses can be used either at compile time or run-time to locate whichever specific versions of the method are needed. §This allows “polymorphism.” §Specifically, it allows “polymorphic method calls.”

2. Polymorphism. §What does this mean? §Literally “poly” means…. §A parrot? §Polytheism = §So “poly” means…. §“Morphic” has to do with…. §So polymorphic means……

Polymorphism (cont.). §E.g. in Greek mythology, Proteus the shape- changer. §E.g. in biology: influenza viruses. §E.g. in computer science: a multi-platform compiler, can take same source code and generate different target programs. §E.g. in missions, same Gospel translated into many languages / cultural categories.

Polymorphism (cont.). §In this context, a method call is polymorphic if a syntactically identical method call can have many different meanings. The ambiguity is resolved by identifying the class to which it belongs. §Why a big deal? The same method call could be re-used for 1000 classes!

Polymorphism (cont.). §E.g. suppose a vast multi-national company has 1000 different types of employee, so 1000 employee classes and all need CalculatePay() and Print() method. §The same method call could be re-used for all of them, instead of having 1000 similar method calls.

3. Detailed worked example. §How can this be done technically? §(1) First select methods to be marked virtual in the base class (they do not all have to be). See Employee_Class_2.cs §E.g. within the Employee2 class: § public virtual void CalculatePay() § public virtual void Print()

Detailed worked example. §(2) In descendant classes, use the same method name marked with override: §E.g. within Hourly and Salaried, see: §public override void CalculatePay() §public override Print () §Note that the implementations are different in different classes.

Detailed worked example. §(3) In the driver class file (Employee_Driver_2. cs), note the polymorphic method calls. The same method name can have a different meaning depending on the class it belongs to. §In this example, early binding is used (the compiler can tell which class a method call refers to).

How do we do late binding? §See Employee_Driver_2A.cs. §Note the use of an Array of objects. §In this case, the meaning of the method calls for CalculatePay() and Print() is determined at run-time using the address of the class. §This is an example of late binding.

How is this possible? §See Employee_Class_2A.cs. §Set_Data is an example of an overloaded method in the base class. §Overloading is not the same as polymorphism because the method calls are not the same for the different versions. §Calculate_Pay() and Print () are polymorphic methods.

4. Abstract classes. §We mentioned before that sometimes it makes no sense for a class to have instances. No employee is just an employee, no animal is just an animal, no shape is just a shape. §To avoid someone instantiating an object of such an abstract class we can mark the class abstract.

Abstract classes (cont.). §As a result, the Employ class cannot be instantiated, but its descendant classes can, SO LONG AS they are not marked abstract (i.e. being abstract is not inherited).