New version page

UNCA CSCI 431 - Dynamic Binding

This preview shows page 1-2-23-24 out of 24 pages.

View Full Document
View Full Document

End of preview. Want to read all 24 pages?

Upload your study docs or become a GradeBuddy member to access this document.

View Full Document
Unformatted text preview:

Dynamic Binding (Section 10.4)Fundamental Concepts in OOPInheritanceInheritance NotationDynamic Method BindingSlide 6Slide 7Slide 8Slide 9Virtual and Non-Virtual MethodsSlide 11Slide 12Abstract ClassesMember LookupSlide 15Slide 16Slide 17Type CheckingSlide 19Type Checking – ImplementationSlide 21Classes as "Closures"Slide 23Slide 2411Dynamic BindingDynamic Binding(Section 10.4)(Section 10.4)CSCI 431 Programming LanguagesCSCI 431 Programming LanguagesFall 2003Fall 2003A compilation of material developed by Felix A compilation of material developed by Felix Hernandez-Campos and Hernandez-Campos and Mircea NicolescuMircea Nicolescu22Fundamental Concepts in OOPFundamental Concepts in OOP•EncapsulationEncapsulation–Data AbstractionData Abstraction–Information hidingInformation hiding–The notion of class and objectThe notion of class and object•InheritanceInheritance–Code reusabilityCode reusability–Is-a vs. has-a relationshipsIs-a vs. has-a relationships•PolymorphismPolymorphism–Dynamic method bindingDynamic method binding33InheritanceInheritance•Encapsulation improves code reusabilityEncapsulation improves code reusability–Abstract Data TypesAbstract Data Types–ModulesModules–ClassesClasses•However, it is generally the case that the code a However, it is generally the case that the code a programmer wants to reuse is close but not exactly programmer wants to reuse is close but not exactly what the programmer needswhat the programmer needs•InheritanceInheritance provides a mechanism to extend or provides a mechanism to extend or refine units of encapsulationrefine units of encapsulation–By adding or By adding or overridingoverriding methods methods–By adding attributesBy adding attributes44InheritanceInheritanceNotationNotationJava.awt.DialogJava.awt.DialogJava.awt.FileDialogJava.awt.FileDialogBase ClassBase Class(or Parent Class(or Parent Classor Superclass)or Superclass)Derived ClassDerived Class(or Child Class(or Child Classor Subclass)or Subclass)Is-a relationshipIs-a relationship55Dynamic Method BindingDynamic Method Binding•Consequence of inheritanceConsequence of inheritance–derived class derived class DD has all members of its base class has all members of its base class BB–can use an object of class can use an object of class DD everywhere an object of class everywhere an object of class BB is expected is expected»a form of polymorphisma form of polymorphism•Example (C++):Example (C++):class person { ... };class person { ... };class student : public person { ... };class student : public person { ... };class professor : public person { ... };class professor : public person { ... };student s;student s;professor p;professor p;......person * x = &s;person * x = &s;person * y = &p;person * y = &p;// Both // Both studentstudent and and professorprofessor objects have all properties of objects have all properties of // a // a personperson object object// Both can be used in a // Both can be used in a personperson context context66Dynamic Method BindingDynamic Method Binding•Example (C++):Example (C++):class person { ... };class person { ... };class student : public person { ... };class student : public person { ... };class professor : public person { ... };class professor : public person { ... };void person::print_mailing_label () { ... }void person::print_mailing_label () { ... }student s;student s;professor p;professor p;......person * x = &s;person * x = &s;person * y = &p;person * y = &p;s.print_mailing_label ();s.print_mailing_label ();p.print_mailing_label ();p.print_mailing_label ();// person::print_mailing_label ()// person::print_mailing_label ()// person::print_mailing_label ()// person::print_mailing_label ()77Dynamic Method BindingDynamic Method Binding•Example (C++):Example (C++):–Suppose that we redefine Suppose that we redefine print_mailing_labelprint_mailing_label in both derived classes in both derived classesvoid student::print_mailing_label () { ... }void student::print_mailing_label () { ... }void professor::print_mailing_label () { ... }void professor::print_mailing_label () { ... }student s;student s;professor p;professor p;......person * x = &s;person * x = &s;person * y = &p;person * y = &p;s.print_mailing_label ();s.print_mailing_label ();p.print_mailing_label ();p.print_mailing_label ();// student ::print_mailing_label ()// student ::print_mailing_label ()// professor ::print_mailing_label ()// professor ::print_mailing_label ()–But what about:But what about:x->print_mailing_label ();x->print_mailing_label ();// ??// ??y->print_mailing_label ();y->print_mailing_label ();// ??// ??88Dynamic Method BindingDynamic Method Binding–Two alternatives for choosing the Two alternatives for choosing the method to call:method to call:»according to the types of variables (references) according to the types of variables (references) xx and and yy – – static method bindingstatic method binding (will call the method of (will call the method of personperson in both cases) in both cases)»according to the types of objects according to the types of objects ss and and pp to which to which xx and and yy refer – refer – dynamic dynamic method binding method binding (will call the methods of (will call the methods of student student // professor professor))–Dynamic method binding – central issue to object-oriented programmingDynamic method binding – central issue to object-oriented programming–Example – list of Example – list of personspersons that have overdue library books that have overdue library books»list may contain both list may contain both studentsstudents and and professorsprofessors»traverse the list and print a mailing label - call the appropriate subroutinetraverse the list and print a mailing label - call the appropriate subroutine•Example (C++):Example (C++):student s;student s;professor p;professor p;......person * x = &s;person * x = &s;person * y = &p;person * y = &p;x->print_mailing_label ();x->print_mailing_label ();// ??// ??y->print_mailing_label ();y->print_mailing_label ();// ??// ??99Dynamic Method BindingDynamic Method Binding•Simula, C++, Ada 95Simula, C++, Ada 95–static method binding by defaultstatic method binding by default–run-time overheadrun-time overhead•Smalltalk, Modula-3Smalltalk, Modula-3–dynamic method bindingdynamic method binding•Java, EiffelJava, Eiffel–dynamic method binding by defaultdynamic method binding by


View Full Document
Loading Unlocking...
Login

Join to view Dynamic Binding and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Dynamic Binding and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?