Learned C++ when I started studying Computer Science but there are still so much to learn.

Chapter 5: Object-Oriented Programming

5.1 Software Reuse and Independence

  • Four basic way that a software component can be modified for reuse
    • extension of the data or operations
    • redefinition of one or more of the operations
    • abstraction
    • polymorphism

5.2 Smalltalk

5.3 Java

  • Some of the Java feature include garbage collection and use of references instead of explicit pointers.
  • Basic Elements of Java: Classes, Objects and Methods.
    • Constructor chaining is when one constructor call another constructor with argument.
  • The Java Collection Framework: Interfaces, Inheritance, and Polymorphism
    • An interfaces is a set of operations on objects of a given type. Interfaces serve as the glue that connects components in software systems.
    • If a class implements an interface, any of its subclasses also (implicitly) implement that interface.
    • Abstract class implement common properties and behavior for their subclasses. Concrete classes can be instantiated.
    • listOfStrings and listOfChars above are just List. Same methods can be called on the two List variable even though the two lists they reference have different implementations and different element types. The variable queueOfFloats has been declared Queue<Float> and so the LinkedList that it refers to are using the methods specified in the Queue interface.
List<String> listOfStrings = new LinkedList<String>();
Queue<Float> queueOfFloats = new LinkedList<Float>();
List<Character> listOfChars = new ArrayList<Character>();
  • Dynamic Binding and Static Binding
    • Dynamic binding is when method being called upon an object is looked up by name at runtime.
    • Java complier statically determines which implementation of a method to use when the receiver object’s actual class can be determined at compile time. However, it will not actually generate code for static binding unless the method has been defined as final or static.
      • final method cannot be overridden in a subclass
      • static method call on a class rather than an instance.

5.4 C++

  • Basic Element of C++: Classes, Data Members, and Member Functions
    • Instance variables and methods are both members of the class (data member and member function, respectively).
    • public, protected, or private member are accessible by client code and derived class, only derived class, or none, respectively.
  • Static Binding, Dynamic Binding and Virtual Functions
    • All member functions are statically bound at compile time. Only function defined using the keyword virtual are candidates for dynamic binding.
    • pure virtual declaration in an abstract class by use the keyword virtual along with the function = 0.
    • Example of repeated inheritance (two separate copies of object of class A) vs shared inheritance (using virtual keyword).
class A {...};
class B : public A {...};
class C : public A {...};
class D : public B, public C {...};

A     A
|     |
B     C
 \   /
class A {...};
class B : virtual public A {...};
class C : virtual public A {...};
class D : public B, public C {...};

 /   \
B     C
 \   /

5.5 Design Issues in Object-Oriented Languages

  • Classes vs Types
    • Possible incorporation of classes into the type system.
      • As typeless entities and exclude from type checking (Objective-C).
      • As type constructors and must be incorporated into type checking (C++). Compatibility of objects of descendent classes to objects of ancestor classes complicates type checking (e.g. x is descendent of y, so y = x but flag x = y.)
      • As a type system itself (Smalltalk, Java).
  • Classes vs Modules
    • Code that uses a class becomes dependent on the particular implementation of the class even if it cannot make use of the details of that implementation.
  • Inheritance vs Polymorphism
    • Four basic kinds of polymorphism
      • parametric polymorphism in which type parameters may remain unspecified in declaration (e.g. C++ template).
      • Overloading in which different function or method declarations share the same name and are disambiguated through the use of the types of the parameters in each declaration.
      • Subtype polymorphism in which all the operations of one type can be applied to another type.
      • Inheritance in which is a kind of subtype polymorphism and overloading.

5.6 Implementation Issues in Object-Oriented Languages

  • Implementation of Objects and Methods
    • The allocation of instance variable of super class precede those variable from derived class in the object instance so that the allocation of the variable within the object instance stay the same.
  • Inheritance and Dynamic Binding
    • Allocation of methods as pointer in the allocation of the object instance.

Additional Notes


Other Resources