Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc."— Presentation transcript:

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

2 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

3 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

4 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

5 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 = ; 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

6 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

7 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

8 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

9 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 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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


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

Similar presentations


Ads by Google