12 object oriented programming


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

12 object oriented programming

  1. 1. ICS 313 - Fundamentals of Programming Languages 112. Object-Oriented Programming12.1 IntroductionCategories of languages that support OOP:1. OOP support is added to an existing languageC++ (also supports procedural and data-oriented programming)Ada 95 (also supports procedural and data-oriented programming)CLOS (also supports functional programming)Scheme (also supports functional programming)2. Support OOP, but have the same appearance and use the basicstructure of earlier imperative languagesEiffel (not based directly on any previous language)Java (based on C++)3. Pure OOP languagesSmalltalk
  2. 2. ICS 313 - Fundamentals of Programming Languages 212.2 Object-Oriented ProgrammingParadigm Evolution1. Procedural - 1950s-1970s (procedural abstraction)2. Data-Oriented - early 1980s (data abstraction)3. OOP - late 1980s (Inheritance and dynamic binding)Origins of InheritanceObservations of the mid-late 1980s :Productivity increases can come from reuseUnfortunately, ADTs are difficult to reuse - never quite rightAll ADTs are independent and at the same levelInheritance solves both - reuse ADTs after minorchanges and define classes in a hierarchy12.2 Object-Oriented Programming (continued)OOP Definitions:ADTs are called classesClass instances are called objectsA class that inherits is a derived class or a subclassThe class from which another class inherits is a parent class orsuperclassSubprograms that define operations on objects are calledmethodsThe entire collection of methods of an object is called itsmessage protocol or message interfaceMessages have two parts - a method name and the destinationobjectIn the simplest case, a class inherits all of the entities of itsparent
  3. 3. ICS 313 - Fundamentals of Programming Languages 312.2 Object-Oriented Programming (continued)Inheritance can be complicated by access controls to encapsulated entitiesA class can hide entities from its subclassesA class can hide entities from its clientsA class can also hide entities for its clients while allowing its subclasses to see themBesides inheriting methods as is, a class can modify an inherited methodThe new one overrides the inherited oneThe method in the parent is overridenThere are two kinds of variables in a class:1. Class variables - one/class2. Instance variables - one/objectThere are two kinds of methods in a class:1. Class methods – accept messages to the class2. Instance methods – accept messages to objectsSingle vs. Multiple InheritanceOne disadvantage of inheritance for reuse:Creates interdependencies among classes that complicate maintenance12.2 Object-Oriented Programming (continued)Polymorphism in OOPLsA polymorphic variable can be defined in a class that is able toreference (or point to) objects of the class and objects of any ofits descendantsWhen a class hierarchy includes classes that override methodsand such methods are called through a polymorphic variable,the binding to the correct method MUST be dynamicThis polymorphism simplifies the addition of new methodsA virtual method is one that does not include a definition (it onlydefines a protocol)A virtual class is one that includes at least one virtual methodA virtual class cannot be instantiated
  4. 4. ICS 313 - Fundamentals of Programming Languages 412.2 Object-Oriented Programming (continued)Design Issues for OOPLs1. The Exclusivity of Objectsa. Everything is an objectAdvantage - elegance and purityDisadvantage - slow operations on simple objects (e.g., float)b. Add objects to a complete typing systemAdvantage - fast operations on simple objectsDisadvantage - results in a confusing type system (two kinds of entities)c. Include an imperative-style typing system for primitives butmake everything else objectsAdvantage - fast operations on simple objects and a relatively small typing systemDisadvantage - still some confusion because of the two type systems2. Are Subclasses Subtypes?Does an is-a relationship hold between a parent class objectand an object of the subclass?12.2 Object-Oriented Programming (continued)3. Implementation and Interface InheritanceIf only the interface of the parent class is visible to the subclass,it is interface inheritanceDisadvantage - can result in inefficienciesIf both the interface and the implementation of the parent classis visible to the subclass, it is implementation inheritanceDisadvantage - changes to the parent class require recompilation of subclasses, andsometimes even modification of subclasses4. Type Checking and PolymorphismPolymorphism may require dynamic type checking ofparameters and the return valueDynamic type checking is costly and delays error detectionIf overriding methods are restricted to having the sameparameter types and return type, the checking can be static
  5. 5. ICS 313 - Fundamentals of Programming Languages 512.2 Object-Oriented Programming (continued)5. Single and Multiple InheritanceDisadvantages of multiple inheritance:Language and implementation complexity (in part due to name collisions)Potential inefficiency - dynamic binding costs more with multiple inheritance (but not much)Advantage:Sometimes it is extremely convenient and valuable6. Allocation and Deallocation of ObjectsFrom where are objects allocated?If they all live in the heap, references to them are uniformSimplifies assignment - dereferencing can be implicitIs deallocation explicit or implicit?7. Dynamic and Static BindingShould ALL binding of messages to methods be dynamic?If none are, you lose the advantages of dynamic bindingIf all are, it is inefficient