OOP And Dynamic Method Binding

Transcription

2/10/2013OOP and Dynamic MethodBindingChapter 9Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding– Polymorphism or Subtype Polymorphism One of three key factors in OOP– Encapsulation of data and methods (data hiding)– Inheritance– Dynamic method binding1

2/10/2013Dynamic Method Binding Ability to use a derived class in a context thatexpects its base classPersonprintLabel()Student s new Student()Professor p new Professor()Person x s;Person y abel();p.printLabel();x.printLabel(); // Which one?y.printLabel();Dynamic Method Bindingx.printLabel(); If we use Person’s printLabel() then this is usingstatic binding– We say that Student’s printLabel() redefines Person’sprintLabel() If we use Student’s printLabel() this this is usingdynamic binding– We say that Student’s printLabel() overrides Person’sprintLabel()– This is what always happens in Java C and C# let you do both2

2/10/2013Virtual Methods Methods that can be overridden are calledvirtual methods– You might never have seen this term before sinceit’s not used in Java because all methods arevirtual In C :class person{public:virtual void printLabel();Normally virtual methods are usedwhen the object doesn’t know whatimplementation is to be used atcompile timeAbstract Classes In most OOP languages we can omit the bodyof a virtual method in a base class Java and C# use the keyword abstract– A class defined as abstract must have at least oneabstract method on itabstract class person {public abstract void printLabel(); C uses assignment to 0class person {public:An interface isidentical to anabstract class with allabstract methodsvirtual void printLabel() 0;3

2/10/2013Dynamic Method Binding Non-virtual methods require no space at runtime; the compiler just calls the appropriateversion, based on type of variable– Member functions are passed an extra, hidden,initial parameter: this (called Me in VB and self inSmalltalk) C philosophy is to avoid run-time overheadwhenever possible(Sort of the legacy from C)– Languages like Smalltalk have (much) more runtime supportDynamic Method Binding Virtual functions are the only thing thatrequires any trickiness– They are implemented by creating a dispatch table(vtable) for the class and putting a pointer to thattable in the data of the object– Objects of a derived class have a different dispatchtable In the dispatch table, functions defined in the parentcome first, though some of the pointers point tooverridden versions4

2/10/2013Implementation of Virtual Methodsvtable virtual method tableImplementation of Virtual Methods5

2/10/2013Different on Stack:Foo *f new Foo();Bar *b new Bar();Foo *q b;Bar *s f;// static semantic errorFoo f;Bar b;Foo q b;Bar s f; // ErrorDynamic Type Binding Note that if you can query the type of anobject, then you need to be able to get fromthe object to run-time type info– The standard implementation technique is to puta pointer to the type info at the beginning of thevtable– Of course you only have a vtable in C if yourclass has virtual functions6

Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding –Polymorphism or Subtype Polymorphism One of three key factors in OOP –Encap