Object Oriented Programming LanguagesWhat are the benefits?What is an OOPL?Common featuresOther featuresClassesC++ Example:InstantiationInstantiation ExampleInheritanceSubclassing ¹ subtyping ¹ is aJava ExampleMethod lookup (binding)Multiple InheritanceProtectionSlide 16Object Oriented Programming LanguagesWhat are the benefits?Systems more adaptive to changeInformation hidingEncapsulationAppeals to human cognitionReuseGamma et al.: Design PatternsMethodologies for design and analysisGrady Booch: Object Oriented Design & AnalysisCASE toolsWhat is an OOPL?No consensusCardelli + Wegner: OO = ADTs + first class objects + types inheriting attributes from a supertypeFinkel: OO = encapsulation + inheritance + overloadingCommon featuresClassesObjects (instantiation)(Single) Inheritance + redefinitionType inheritanceMultiple inheritanceMethod overloadingDynamic lookupDeferred implementationsProtectionOther featuresStatic typing, Genericity - Unit 8Exception handling, Garbage collection, Design by contract - Unit 9Multiple polymorphismReflection + Meta-object protocolsPackagesClass methodsIteratorsOperator overloadingClassesA class is “a software element that describes an ADT and its partial or total definition” (Meyer)Can be viewed as a set of members (or slots), which can be:Data (attributes or fields) Operations (methods, messages)C++ Example:class FixedIntStack {int index, *arr;public: FixedIntStack(int size) { index = 0; arr = new int[size]; }~FixedIntStack() { delete arr; }void push(int item){ arr[index++] = item; }int pop() { return arr[--index]; }};InstantiationClass instances are called objects, and are first class entities.Constructors run at instantiation time.Arguments can be passed to instantiations.Instantiation is usually handled by an operator! In Eiffelnew in C++ and JavaDeallocation may call a destructor (or finalization method)Instantiation ExampleC++FixedIntStack *stack1 = new FixedIntStack(10);FixedIntStack stack2(10);stack1->push(5);stack2.push(5);JavaFixedIntStack myStack = new FixedIntStack(10);myStack.push(5);A trend: objects PBR, primitive types by valueInheritanceIncremental program extension class Integer inherit NumberNumber is the parent of IntegerInteger is the child or descendant of Numbermultiple inheritanceclass Mobile_Home inherit Vehicle, HouseSubclassing subtyping is aSubclassing - code & data sharingSubtyping - type sharing substitutability - a subtype may stand in for any parent type polymorphism - void print(Object ob)Specialization (is a) - implies subtyping and subclassingJava Exampleclass Rectangle { // default: extends Object float width, height; void set_width(float w) { width = w; } void set_height(float h) { height = h; }}class Square extends Rectangle { // These are overridden (redefined) methods: void set_width(float w) { super.set_width(w); super.set_height(w); } void set_height(float w) { set_width(float w); }Method lookup (binding)Square s = new Square();s.set_width(12); // Meaning is obviousRectangle r = s; // substitutability!r.set_width(12);Dynamic lookup: Square’s method is called.Static lookup: Rectangle’s method is calledJava: Dynamic LookupC++: virtual keywordLiskov substitution principleMultiple InheritanceIOStreamOStreamIStreamMore from John Viega next Tuesday...ProtectionA way to enforce encapsulation. C++:no change by defaultprivate, public, protected membersprivate, public, protected inheritancebreak the rules with friendclass X : protected Y, private Z { friend class Q; public:X(); private: int datum;}ProtectionEiffel:No change by defaultsecret + non-secret membersselectively export operations to classesclass MySecrets inherit GovtSecrets export {NONE} really_big_secret {ANY} common_knowledgefeature {NSA, CIA, FBI} big_secret :
View Full Document