9. Object oriented programming

A. What is OOP?

Object oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. The key idea is to structure the program into reusable pieces of code, which are used to create individual instances.

  • Class: An abstract blueprint used to create more specific, concrete objects.
    • They have shared attributes
    • classes can also contain functions called methods, available only to objects of that class
  • Object: An instance of a class.

B. Why use OOP?

  1. Models complex things as reproducible, simple structures.
  2. Reusable.
  3. Secure, protects information through encapsulation.

C. Four principles of OOP

  1. Abstraction: Exposing only high level public methods for accessing an object.
  2. Encapsulation: Hiding implementation details from the user and exposing only selected information.
  3. Inheritance: Child classes inherit data and behaviours from parent class.
  4. Polymorphism: Designing objects to share behaviours. Polymorphism allows the same method to behave differently depending on the type of object.
    • Method overriding: A child class can provide a different implementation than its parent class.
    • Method overloading: Methods or functions can have same name, but different number and type of parameters.

D. Other concepts

  • virtual functions: Functions that are present in the parent class and are overridden by the subclass.
  • Constructor: A special type of method that has the same name as the class and is used to initialize objects of that class.