UVA CS 415 - Data Abstraction and Object Orientation

Unformatted text preview:

Chapter 10: Data Abstraction and Object OrientationFundamental OO ConceptsEncapsulationModulesModule ChangingClasses can limit visibilityDerived class can restrict visibilityInitialization and FinalizationFour Important IssuesChoosing a ConstructorConstructorsReferences and ValuesExecution OrderDestructors and Garbage CollectionJava’s finalize() methodDynamic Method BindingPolymorphismPolymorphism ExampleDynamic vs. Static bindingWhich one does Java use?Dynamic method bindingSlide 22Abstract MethodsAbstract ClassesMember Lookup: vtableMember Lookup- vtableSingle InheritanceMultiple InheritanceSlide 29Slide 30Why use MI?Multiple inheritance typesNormal (non-repeated) MISlide 34Efficiency (or lack thereof)Semantic AmbiguitiesSlide 37Replicated Multiple InheritanceSlide 39Shared Multiple InheritanceSlide 41Mix-in InheritanceJava Interfaces1Chapter 10: Data Abstraction and Object OrientationAaron BloomfieldCS 415Fall 20052Fundamental OO Concepts•Encapsulation•Inheritance•Dynamic Method Binding3Encapsulation•Encapsulation–Encapsulation allows the programmer to group data and the subroutines that operate on them together in one place, and to hide irrelevant details from the user.•Information Hiding–Making objects and algorithms invisible to portions of the system that do not need them.4Modules•If a module M exports a type T, the rest of the program can only pass T to subroutines exported from M.–T is said to be an opaque type. var Database : module exports (tuple with (:=, name)) … type tuple = record var name : packed array 1..80 of char… end tuple…•What can the code outside the Database module do?5Module Changing•Body is Changed•Private Part of Header is Changed•Public Part of Header is Changed6Classes can limit visibility•Private•Protected•Public•Package (in some languages, e.g. Java)7Derived class can restrict visibility•Private–Protected and public members of base class are private in derived class.•Protected–Protected and public members of base class are protected in derived class.•Public–Protected and public members of base class are protected and public in derived class.•Private members of base class aren’t visible in derived class.8Initialization and Finalization9Four Important Issues•Choosing a Constructor•References and Values•Execution Order•Garbage Collection–We’ve seen that already10Choosing a Constructor•Object-Oriented Languages allow classes to have zero, one or more different constructors.•Two ways to distinguish between constructors–Different Names–Different Number and Types of Arguements11Constructors•Eiffel code:•class COMPLEXcreationnew_cartesian, new_polar…new_cartesian(x_val, y_va; : REAL) is…new_polar(rho, theta : REAL) is…•class mydata {public:mydata(string data);mydata(int data);mydata();};12References and Values•C++ vs. Java–Java uses reference, C++ you can specify•Reference–Every object is created explicitly so it is easy to make sure the correct constructor is called.–More elegant, but requires allocation from heap and extra indirections on every access of the object.•Value–More efficient but harder to control initialization13Execution Order•If class B is derived from class A, A constructor is called before B constructor–To get arguments to the A constructor, you must use an intializer listclass foo : bar {...}foo::foo (foo_params) : bar(bar_params) {…–The part after the colon is a call to bar’s constructor14Destructors and Garbage Collection•When an object is destroyed, the destructor is called for the derived class first, then the destructors of the base classes are called.–Reverse order of derivation•Destructors purpose is to return allocated space back to the heap•Many languages provide automatic garbage collection–Java, Smalltalk, Eiffel, etc.15Java’s finalize() method•In Java, you can override the finalize() method•This allows code to be executed when the object is about to be deleted–But you shouldn’t extend the object’s lifetime by doing this–As the finalize() method is only called once per object16Dynamic Method Binding17Polymorphism•A derived class (D) has all the members of its base class (C)–Class D can be used anytime class C is expected.–If class D does not hide any publicly visible members of C then D is a subtype of C.•If class D is used in place of class C, this is a form of polymorphism.18Polymorphism Exampleclass person { …class student : public person { …class professor : public person { …student s;professor p;…person *x = &s;person *y = &p;19Dynamic vs. Static binding•Static method binding uses the type of the reference: s.print_mailing_label();p.print_mailing_label();•Dynamic method binding uses the class of the object that is referred/pointed to:x->print_mailing_label();y->print_mailing_label();20Which one does Java use?public class Foo { public String toString() { return "Foo's toString()"; } public static void main (String args[]) { Object bar = new Foo(); System.out.println (bar); }}•Java uses dynamic binding21Dynamic method binding•Dynamic method binding: calls to virtual methods are dispatched to the appropriate implementation at run time based on the class of the object–Simula: virtual methods listed at beginning of class declarationCLASS Person;VIRTUAL: PROCEDURE PrintMailingLabel;BEGIN…END Person;22Dynamic method binding–C++: keyword “virtual” prefixes function declarationclass person {public:virtual void print_mailing_label ();…}•This requires keeping a virtual method table along with each object–More on this in a bit…23Abstract Methods•Bodyless virtual methodsIn C++: called pure virtual method, created by following a procedure declaration with an assignment to zero.class person {…public:virtual void print_mailing_label() = 0;24Abstract Classes•Class that contains one or more abstract methods–Java: called an interface (which has only abstract methods)•Generally not possible to declare object of an abstract class b/c it would be missing at least one member–But you can do so in C++•Serves as a base for concrete classes.–Concrete class must provide a definition for every abstract method it inherits•Application to dynamic method binding: allows code that calls methods of objects of


View Full Document

UVA CS 415 - Data Abstraction and Object Orientation

Download Data Abstraction and Object Orientation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data Abstraction and Object Orientation 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 Data Abstraction and Object Orientation 2 2 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?