Introduction

  • Many object-oriented programming (OOP) languages

–Some support procedural and data-oriented programming (e.g., Ada 95+ and C++)

–Some support functional program (e.g., CLOS)

–Newer languages do not support other paradigms but use their imperative structures (e.g., Java and C#)

–Some are pure OOP language (e.g., Smalltalk & Ruby)

–Some functional languages support OOP, but they are not discussed in this chapter.

Three major language features:

–Abstract data types (Chapter VIII)

–Inheritance

  • Inheritance is the central theme in OOP and languages that support it

–Polymorphism

Inheritance

  • Productivity increases can come from reuse

–ADTs are difficult to reuse—always need changes

–All ADTs are independent and at the same level

  • Inheritance allows new classes defined in terms of existing ones, i.e., by allowing them to inherit common parts
  • Inheritance addresses both of the above concerns–reuse ADTs after minor changes and define classes in a hierarchy
  • 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

    –The new one overrides the inherited one

    –The method in the parent is overriden

    Besides inheriting methods as is, a class can modify an inherited method

Object Oriented Concepts

  • ADTs are usually 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
  • Calls to methods are called messages
  • 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

Dynamic Binding

  • 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 will be dynamic
  • Allows software systems to be more easily extended during both development and maintenance

Dynamic Binding Concept

  • An abstract method is one that does not include a definition (it only defines a protocol)
  • An abstract class is one that includes at least one virtual method
  • An abstract class cannot be instantiated

Exclusivity of Objects

  • Everything is an object

–Advantage – elegance and purity

–Disadvantage – slow operations on simple objects

  • Add objects to a complete typing system

–Advantage – fast operations on simple objects

–Disadvantage – results in a confusing type system (two kinds of entities)

  • 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

Nested Classes

  • If a new class is needed by only one class, there is no reason to define so it can be seen by other classes

–Can the new class be nested inside the class that uses it?

–In some cases, the new class is nested inside  a subprogram rather than directly in another class

  • Other issues:

–Which facilities of the nesting class should be visible to the nested class and vice versa

Dynamic Binding of Methods Calls

  • Methods in a class that are statically bound need not be involved in the CIR; methods that will be dynamically bound must have entries in the CIR

–Calls to dynamically bound methods can be connected to the corresponding code thru a pointer in the CIR

–The storage structure is sometimes called virtual method tables  (vtable)

–Method calls can be represented as offsets from the beginning of the vtable.