ICS 313 - Fundamentals of Programming Languages 1 12. Object-Oriented Programming 12.1 Introduction Categories of languages that support OOP: 1. OOP support is added to an existing language C++ (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 basic structure of earlier imperative languages Eiffel (not based directly on any previous language) Java (based on C++) 3. Pure OOP languages Smalltalk
ICS 313 - Fundamentals of Programming Languages 2 12.2 Object-Oriented Programming Paradigm Evolution 1. Procedural - 1950s-1970s (procedural abstraction) 2. Data-Oriented - early 1980s (data abstraction) 3. OOP - late 1980s (Inheritance and dynamic binding) Origins of Inheritance Observations of the mid-late 1980s : Productivity increases can come from reuse Unfortunately, ADTs are difficult to reuse - never quite right All ADTs are independent and at the same level Inheritance solves both - reuse ADTs after minor changes and define classes in a hierarchy 12.2 Object-Oriented Programming (continued) OOP Definitions: ADTs are called classes Class instances are called objects A class that inherits is a derived class or a subclass The class from which another class inherits is a parent class or superclass Subprograms that define operations on objects are called methods The entire collection of methods of an object is called its message protocol or message interface Messages have two parts - a method name and the destination object In the simplest case, a class inherits all of the entities of its parent
ICS 313 - Fundamentals of Programming Languages 3 12.2 Object-Oriented Programming (continued) Inheritance can be complicated by access controls to encapsulated entities A class can hide entities from its subclasses A class can hide entities from its clients A class can also hide entities for its clients while allowing its subclasses to see them Besides inheriting methods as is, a class can modify an inherited method The new one overrides the inherited one The method in the parent is overriden There are two kinds of variables in a class: 1. Class variables - one/class 2. Instance variables - one/object There are two kinds of methods in a class: 1. Class methods – accept messages to the class 2. Instance methods – accept messages to objects Single vs. Multiple Inheritance One disadvantage of inheritance for reuse: Creates interdependencies among classes that complicate maintenance 12.2 Object-Oriented Programming (continued) Polymorphism in OOPLs A polymorphic variable can be defined in a class that is able to reference (or point to) objects of the class and objects of any of its descendants When a class hierarchy includes classes that override methods and such methods are called through a polymorphic variable, the binding to the correct method MUST be dynamic This polymorphism simplifies the addition of new methods A virtual method is one that does not include a definition (it only defines a protocol) A virtual class is one that includes at least one virtual method A virtual class cannot be instantiated
ICS 313 - Fundamentals of Programming Languages 4 12.2 Object-Oriented Programming (continued) Design Issues for OOPLs 1. The Exclusivity of Objects a. Everything is an object Advantage - elegance and purity Disadvantage - slow operations on simple objects (e.g., float) b. Add objects to a complete typing system Advantage - fast operations on simple objects Disadvantage - results in a confusing type system (two kinds of entities) c. Include an imperative-style typing system for primitives but make everything else objects Advantage - fast operations on simple objects and a relatively small typing system Disadvantage - still some confusion because of the two type systems 2. Are Subclasses Subtypes? Does an is-a relationship hold between a parent class object and an object of the subclass? 12.2 Object-Oriented Programming (continued) 3. Implementation and Interface Inheritance If only the interface of the parent class is visible to the subclass, it is interface inheritance Disadvantage - can result in inefficiencies If both the interface and the implementation of the parent class is visible to the subclass, it is implementation inheritance Disadvantage - changes to the parent class require recompilation of subclasses, and sometimes even modification of subclasses 4. Type Checking and Polymorphism Polymorphism may require dynamic type checking of parameters and the return value Dynamic type checking is costly and delays error detection If overriding methods are restricted to having the same parameter types and return type, the checking can be static
ICS 313 - Fundamentals of Programming Languages 5 12.2 Object-Oriented Programming (continued) 5. Single and Multiple Inheritance Disadvantages 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 valuable 6. Allocation and Deallocation of Objects From where are objects allocated? If they all live in the heap, references to them are uniform Simplifies assignment - dereferencing can be implicit Is deallocation explicit or implicit? 7. Dynamic and Static Binding Should ALL binding of messages to methods be dynamic? If none are, you lose the advantages of dynamic binding If all are, it is inefficient

12 object oriented programming

  • 1.
    ICS 313 -Fundamentals of Programming Languages 1 12. Object-Oriented Programming 12.1 Introduction Categories of languages that support OOP: 1. OOP support is added to an existing language C++ (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 basic structure of earlier imperative languages Eiffel (not based directly on any previous language) Java (based on C++) 3. Pure OOP languages Smalltalk
  • 2.
    ICS 313 -Fundamentals of Programming Languages 2 12.2 Object-Oriented Programming Paradigm Evolution 1. Procedural - 1950s-1970s (procedural abstraction) 2. Data-Oriented - early 1980s (data abstraction) 3. OOP - late 1980s (Inheritance and dynamic binding) Origins of Inheritance Observations of the mid-late 1980s : Productivity increases can come from reuse Unfortunately, ADTs are difficult to reuse - never quite right All ADTs are independent and at the same level Inheritance solves both - reuse ADTs after minor changes and define classes in a hierarchy 12.2 Object-Oriented Programming (continued) OOP Definitions: ADTs are called classes Class instances are called objects A class that inherits is a derived class or a subclass The class from which another class inherits is a parent class or superclass Subprograms that define operations on objects are called methods The entire collection of methods of an object is called its message protocol or message interface Messages have two parts - a method name and the destination object In the simplest case, a class inherits all of the entities of its parent
  • 3.
    ICS 313 -Fundamentals of Programming Languages 3 12.2 Object-Oriented Programming (continued) Inheritance can be complicated by access controls to encapsulated entities A class can hide entities from its subclasses A class can hide entities from its clients A class can also hide entities for its clients while allowing its subclasses to see them Besides inheriting methods as is, a class can modify an inherited method The new one overrides the inherited one The method in the parent is overriden There are two kinds of variables in a class: 1. Class variables - one/class 2. Instance variables - one/object There are two kinds of methods in a class: 1. Class methods – accept messages to the class 2. Instance methods – accept messages to objects Single vs. Multiple Inheritance One disadvantage of inheritance for reuse: Creates interdependencies among classes that complicate maintenance 12.2 Object-Oriented Programming (continued) Polymorphism in OOPLs A polymorphic variable can be defined in a class that is able to reference (or point to) objects of the class and objects of any of its descendants When a class hierarchy includes classes that override methods and such methods are called through a polymorphic variable, the binding to the correct method MUST be dynamic This polymorphism simplifies the addition of new methods A virtual method is one that does not include a definition (it only defines a protocol) A virtual class is one that includes at least one virtual method A virtual class cannot be instantiated
  • 4.
    ICS 313 -Fundamentals of Programming Languages 4 12.2 Object-Oriented Programming (continued) Design Issues for OOPLs 1. The Exclusivity of Objects a. Everything is an object Advantage - elegance and purity Disadvantage - slow operations on simple objects (e.g., float) b. Add objects to a complete typing system Advantage - fast operations on simple objects Disadvantage - results in a confusing type system (two kinds of entities) c. Include an imperative-style typing system for primitives but make everything else objects Advantage - fast operations on simple objects and a relatively small typing system Disadvantage - still some confusion because of the two type systems 2. Are Subclasses Subtypes? Does an is-a relationship hold between a parent class object and an object of the subclass? 12.2 Object-Oriented Programming (continued) 3. Implementation and Interface Inheritance If only the interface of the parent class is visible to the subclass, it is interface inheritance Disadvantage - can result in inefficiencies If both the interface and the implementation of the parent class is visible to the subclass, it is implementation inheritance Disadvantage - changes to the parent class require recompilation of subclasses, and sometimes even modification of subclasses 4. Type Checking and Polymorphism Polymorphism may require dynamic type checking of parameters and the return value Dynamic type checking is costly and delays error detection If overriding methods are restricted to having the same parameter types and return type, the checking can be static
  • 5.
    ICS 313 -Fundamentals of Programming Languages 5 12.2 Object-Oriented Programming (continued) 5. Single and Multiple Inheritance Disadvantages 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 valuable 6. Allocation and Deallocation of Objects From where are objects allocated? If they all live in the heap, references to them are uniform Simplifies assignment - dereferencing can be implicit Is deallocation explicit or implicit? 7. Dynamic and Static Binding Should ALL binding of messages to methods be dynamic? If none are, you lose the advantages of dynamic binding If all are, it is inefficient