Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.

Slides:



Advertisements
Similar presentations
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Advertisements

Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
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.
Polymorphism &Virtual Functions
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Lecture 9 Polymorphism Richard Gesick.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Java Implementation: Part 3 Software Construction Lecture 8.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Introduction to Object-Oriented Programming Lesson 2.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Chapter 12 OOP: Polymorphism, Interfaces and Operator Overloading
Polymorphism, Interfaces & Operator Overloading
16: Equals Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Ch 10- Advanced Object-Oriented Programming Features
7. Inheritance and Polymorphism
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
15: Object Object Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Methods Attributes Method Modifiers ‘static’
Polymorphism.
Object-Oriented Programming: Polymorphism
Designing for Inheritance
Lecture 23 Polymorphism Richard Gesick.
Chapter 9 Inheritance and Polymorphism
Programming with ANSI C ++
Java Programming Language
Virtual Functions Department of CSE, BUET Chapter 10.
Polymorphism, Abstract Classes & Interfaces
Polymorphism Polymorphism
Java Inheritance.
Polymorphism and Type Casting
Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Abstract Classes and Interfaces
VIRTUAL FUNCTIONS RITIKA SHARMA.
Fundaments of Game Design
Interface 11: Interface Programming C# © 2003 DevelopMentor, Inc.
COP 3330 Object-oriented Programming in C++
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:

Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Objectives Discuss type compatibility in the presence of inheritance 10: Binding Objectives Discuss type compatibility in the presence of inheritance Contrast ways to bind method call static dynamic Cover details of dynamic binding virtual methods abstract methods override polymorphism and generic code Use sealed to prevent inheritance and method overriding Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

10: Binding Is-a relationship Is-a means derived has everything base does, plus more class Person { public string name public int age; ... } Employee is-a Person class Employee : Person { public double salary; ... } Employee e = new Employee(); name age salary Person part e Employee part Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Reference compatibility 10: Binding Reference compatibility Base reference can refer to derived object types are compatible because of is-a relationship Person p = new Employee(); Person reference Employee object name age salary p Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Access through base reference 10: Binding Access through base reference Type of reference determines what access is allowed can only access base class members through base reference Person reference to Employee object Person p = new Employee(); p.name = "Bob"; p.age = 23; p.salary = 42000.00; ok to access Person members error to access Employee members name age salary only Person part accessible p Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Parameter compatibility 10: Binding Parameter compatibility Can pass derived object to base reference parameter base reference bool CheckAge(Person p) { return p.age >= 21; } Student s = new Student (); Employee e = new Employee(); Evaluate(s); Evaluate(e); pass Student pass Employee Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Methods in inheritance hierarchy 10: Binding Methods in inheritance hierarchy Common for same method to appear throughout hierarchy each class supplies own unique version class Employee : Person { public void Promote() { } ... class Faculty : Employee { public void Promote() { salary *= 1.2; level++; if (level > 4) tenure = true; } ... class Staff : Employee { public void Promote() { salary *= 1.1; } ... Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Binding Can call method through base class reference must decide whether to call base or derived version decision often called binding void Evaluate(Employee e) { e.Promote(); } which version? Faculty f = new Faculty(); Staff s = new Staff (); Evaluate(f); Evaluate(s); pass Faculty pass Staff Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Binding options Programmer chooses desired type of binding when method called through base class reference Two options available static dynamic Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

10: Binding Static binding Static binding uses type of reference to determine method default behavior void Evaluate(Employee e) { e.Promote(); } static binding calls Employee Promote Faculty f = new Faculty(); Staff s = new Staff (); Evaluate(f); Evaluate(s); pass Faculty pass Staff Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Dynamic binding Dynamic binding calls method based on type of object programmer must request dynamic binding use keyword virtual in base class use keyword override in derived class void Evaluate(Employee e) { e.Promote(); } class Employee : Person { public virtual void Promote() {} } binding now based on object type Faculty f = new Faculty(); Staff s = new Staff (); Evaluate(f); Evaluate(s); class Faculty : Employee { public override void Promote() { ... } } Faculty Staff class Staff : Employee { public override void Promote() { ... } } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Generic code Dynamic binding used to write generic code dynamic behavior often called polymorphism generic void Evaluate(Employee[] employees) { foreach (Employee e in employees) e.Promote(); } Employee[] employees = new Employee[3]; employees[0] = new Faculty(); employees[1] = new Staff (); employees[2] = new Faculty(); Evaluate(employees); various types Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Extending type hierarchy 10: Binding Extending type hierarchy Generic code works with new types added to hierarchy no changes needed new type class Administrator : Employee { public override void Promote() { ... } } generic code unchanged void Evaluate(Employee[] employees) { foreach (Employee e in employees) e.Promote(); } Employee[] employees = new Employee[3]; employees[0] = new Faculty (); employees[1] = new Staff (); employees[2] = new Administrator(); Evaluate(employees); Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Binding efficiency Static binding is efficient type of reference known at compile time binding done at compile time no runtime overhead Dynamic binding is less efficient use incurs small runtime overhead object type must be determined at runtime Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Abstract class Sometimes class represents abstract concept 10: Binding Abstract class Sometimes class represents abstract concept can indicate by making class abstract abstract class abstract class Employee : Person { ... } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

10: Binding Abstract method Sometimes no sensible method implementation in base class but still must appear in base class interface Can omit implementation by making abstract header only, no implementation implicitly virtual class containing abstract method required to be abstract abstract class Employee : Person { public abstract void Promote(); ... } abstract method Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Specification Abstract method is a specification 10: Binding Specification Abstract method is a specification imposes requirement on all derived classes derived classes must implement or will be abstract abstract class Employee : Person { public abstract void Promote(); ... } specification class Faculty : Employee { public override void Promote() { ... } } implementation class Staff : Employee { public override void Promote() { ... } } implementation Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Limitation on abstract class 10: Binding Limitation on abstract class Abstract class cannot be instantiated but use of references still ok ok Employee e; e = new Employee(); error Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Limitation of generic code 10: Binding Limitation of generic code Access through base reference restricted to base members even when referenced object is of derived type Employee e = new Faculty(); e.Promote(); e.level++; Faculty object, Employee reference can only access Employee members error, no access to Faculty members Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Downcast Sometimes need to obtain more specific reference 10: Binding Downcast Sometimes need to obtain more specific reference to get access to entire object Conversion from base to derived requires downcast can use cast syntax can use operator as Employee e = new Faculty(); Faculty f = e; Faculty object, Employee reference error, no implicit conversion Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Cast syntax Can downcast using cast type name in parentheses 10: Binding Cast syntax Can downcast using cast type name in parentheses yields appropriate reference if cast succeeds throws InvalidCastException if types are not compatible Use cast syntax when: cast is expected to succeed exception would be appropriate response to failure void Evaluate(Employee e) { Faculty f = (Faculty)e; f.level++; } cast access Faculty members Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Operator as Can downcast using operator as 10: Binding Operator as Can downcast using operator as yields appropriate reference if cast succeeds null reference if types are not compatible Use operator as when: it is reasonable and expected that the cast might fail exception would not be an appropriate response to failure void Evaluate(Employee e) { Faculty f = e as Faculty; if (f != null) f.level++; } cast test access Faculty members Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Casting issues Downcast sometimes considered poor style 10: Binding Casting issues Downcast sometimes considered poor style makes code less generic compiler often cannot determine if cast will succeed checking generally put off until runtime possible for runtime failure Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Type testing Can test type of object operator is 10: Binding Type testing Can test type of object operator is useful to test before cast to ensure cast will succeed void Evaluate(Employee e) { if (e is Faculty) ... } test Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Preventing inheritance 10: Binding Preventing inheritance Can prevent inheritance using keyword sealed compile time error to attempt derivation from sealed class shows designer believes class should not be extended sealed sealed class String { ... } error, can not derive from sealed class class MyString : String { ... } Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Preventing method override 10: Binding Preventing method override Can prevent further overrides of method using sealed can only be applied to method labeled as override further overrides prevented in derived classes class A { public virtual void Method() { ... } } not override so can not be sealed class B : A { public sealed override void Method() { ... } } can seal at this level class C : B { public override void Method() { ... } } error, can not override further Programming C# © 2003 DevelopMentor, Inc. 12/1/2003

Summary Programmer can choose method binding type static dynamic Choice of binding involves trade offs static binding more efficient dynamic binding used to write more generic code Can use abstract to model abstract method or class Can use sealed to prevent inheritance or method override Can cast to recover more specific reference Programming C# © 2003 DevelopMentor, Inc. 12/1/2003