Skip to content

6. OOP

1 Lecture Notes

  • Information Hiding: The internal state of the object is hidden from the outside world and only manipulated through the object’s methods.
  • Encapsulation: The internal state and methods are tied together and hidden from the outside world.
  • Implementation: The details of the objects, including the data structure of its state and the code of its methods.
  • Interface and Implementation are independent of each other.
  • Message Passing:
    • Objects communicate with each other by sending messages.
    • Messages are requests for services or method invocations.
  • Inheritance:
    • Subclass (child, derived) inherits Superclass(es) (base, parent)
    • Constructors are NOT inherited, but it can be called or referenced through super().
  • Polymorphism:
    • It means many forms.
    • The same method has different implementations in different classes, but the method signature is the same.
    • The same method has different signatures in the same class.
    • The compiler decides which method to call based on the object type.
    • Static polymorphism:
      • The compiler decides which method to call at compile time.
      • Also known as early binding or compile-time binding.
    • Dynamic polymorphism:
      • The compiler decides which method to call at run time.
      • Also known as late binding or run-time binding, or runtime dispatching.
  • Abstract Class:
    • A class that cannot be instantiated, but inherited.
    • Promotes code reuse.
  • Concrete Class:
    • A class that can be instantiated.
    • Provides specific implementation of the abstract class(es) and/or interface(s)
  • Interface:
    • Only contains method signatures, and cannot be instantiated.
    • Purpose is to specify behavior.
  • Generics:
    • A class or method that can be used with different types of objects (parametrized types).
    • Advantages: Reusability, Writablity, Maintainability.
  • Constructor:
    • Initializes the object’s state.
    • Constructors are never inherited.
  • Destructor:
    • Destroys the object’s state and explicitly frees the memory.
    • Different from garbage collection which happens implicitly.

2 OOP

  • A programming language is said to support OOP if it includes constructs for:
    • Encapsulation and data abstraction.
    • Inheritance.
    • Dynamic polymorphism.

Polymorphism

  • The ability to use the a variable, value, or subprogram in more than one form.
  • It is difficult; source of bugs
  • Static Polymorphism: at compile time, machine code is generated for each type of argument.
    • Type conversion: A value of one type is converted to another type.
    • Overloading: The same name is used for two or more different objects or subprograms.
    • Generics: A parameterized type of subprogram is used to create several instances of the subprogram.
  • Dynamic Polymorphism: at runtime
    • Variant and unconstrained records: like a variable of type any in Python.
    • Runtime dispatching: the selection of code to run is made at runtime.

Type Conversion

  • Taking a value of one type and converting it to another type.
  • There are two variants:
    • Translating a value from one type to another valid type: convert Float to Integer.
    • Transferring the value as as uninterpreted bit string: unchecked conversion.

Overloading

  • Using the same name to denote different objects in a single scope.
  • Using the same name in two different scopes is not overloading, and has no problems.
  • The functions must be distinguishable by their type signature which is the number and types of their arguments, not their names.
  • Operator overloading is a special case of function overloading, where the operator is used as the function name: e.g. + is used for addition of integers, reals, and strings.

Generics

  • Arrays, Lists, and Trees are general data structures that should be able to store any type of data.
  • Dynamic polymorphism and memory allocation is required if the data type is not known at compile time or if it is possible to store multiple types of data in the same data structure.
  • However, static polymorphism is more efficient, and preferred if the data type is known at compile time or if the data structure can only store one type of data, such that the implementation can be optimized for that type.
  • Generics is a way to achieve static polymorphism by allowing the programmer to define a template of a subprogram and then to create instances of the template for several types.
  • C does not support generics, but it can be achieved using void pointers and sizeof, although it is not type safe.
  • The generic functionality (template) must be copied for each type.
  • The generic parameters are compile-time parameters and are used by the compiler to generate the correct code for the instance.
  • The parameters form a contract between the code of the generic procedure and the instantiation.
  • C++ supports generics using templates, while Ada uses generics, and both are static polymorphism.
  • Java only supports dynamic polymorphism.

References


  1. UoPeople. (2023). CS4402, Comparative Programming Languages, Week 6: Object-Oriented Programming. Lecture Notes. 

  2. Ben-Ari, M. (2006). Understanding programming languages. Weizman Institute of Science. Chapters 10 + 14 + 15.