C# DownCast vs UpCast.

Slides:



Advertisements
Similar presentations
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Advertisements

CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
Polymorphism What is Polymorphism? Taking Advantage of Polymorphism
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Run-Time Type Identification Jim Fawcett CSE687 – Object Oriented Design Spring 2007.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
JAVA Introduction ● One of the main JAVA design goal is reducing complexity for programmer – Development time is half or less comparing to equivalent C++
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
CE Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.
Programming With Java ICS Chapter 8 Polymorphism.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Polymorphic support in C# Let us now examine third pillar of OOP i.e. Polymorphism.. Recall that the Employee base class defined a method named GiveBonus(),
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
Object Oriented Programming Lecture 11: Polymorphism.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 16.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Chapter 10 Inheritance and Polymorphism
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Introduction to Object-Oriented Programming Lesson 2.
Inheritance and Composition Reusing the code and functionality Unit - 04.
More OOP. Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public.
Peyman Dodangeh Sharif University of Technology Fall 2014.
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
7: Polymorphism Upcasting revisited Forgetting the object type
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Polymorphism, Virtual Methods and Abstract Classes
Ch 10- Advanced Object-Oriented Programming Features
Inheritance and Polymorphism
Polymorphism.
JAVA Introduction ការណែនាំពី Java
Classes in C++ C++ originally called "C with classes":
CS 302 Week 11 Jim Williams, PhD.
Object-Oriented Programming (OOP) Lecture No. 28
Classes in C++ C++ originally called "C with classes":
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Programming II Polymorphism A.AlOsaimi.
Sampath Kumar S Assistant Professor, SECE
Polymorphism and Type Casting
Polymorphism.
Fundaments of Game Design
Java Programming: From the Ground Up
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Advanced Programming in Java
Lecture 6: Polymorphism
Computer Science II for Majors
Presentation transcript:

C# DownCast vs UpCast

Polymorphism Polymorphism is one of the most powerful mechanisms of OOP

Class Shape public class Shape { protected int m_xpos; protected int m_ypos;   public Shape() } public Shape(int x, int y) m_xpos = x; m_ypos = y; public virtual void Draw() Console.WriteLine("Drawing a SHAPE at {0},{1}", m_xpos, m_ypos);

Class Circle public class Circle : Shape { public Circle() }   public Circle(int x, int y) : base(x, y) public override void Draw() Console.WriteLine("Drawing a CIRCLE at {0},{1}", m_xpos, m_ypos); } 

Client Code: Upcast Consider the following statement: Upcast: 1) only cast a class to its base class, 2) only consider the static type of an object There are two type conversions: Shape s = new Circle(100, 100); Which type conversion does upcast belong to?

Downcast Downcast: only change the static type of an object. Example: Shape s = new Circle(100, 100); s.fillCircle(); Circle c; c = (Circle)s; s.fillCircle() Which type conversion does downcast belong to?

More on Downcast Since downcast is not safe, the runtime system checks whether the runtime type of an object is a derived class of the cast type or the cast type itself. How to make your program without a runtime exception foreach (Shape shape in shapes) { shape.Draw();   if (shape is Circle) ((Circle)shape).FillCircle();   if (shape is Square) ((Square)shape).FillSquare(); }