Object Oriented Programming Concept Notes

OOP Concepts & Terms: 

Class: Template/Definition of an object that contains properties and methods.

Object: Instance of a class. An object contains properties and methods as defined in class. Each object of a class may have different values in properties. 

Properties: Variables of a class/object. Variables store information. Also called data of class/object.

Methods: Functions of a class/object. Functions are executable code/logic. These methods operate on data of the class/object. 

Static Property & Method: A property or method that belongs to the whole class instead of an object. 

Constructor: A Constructor is a method of a class that is used to create/initialize a new object of the class.

Destructor: Destructor destroys the object and deletes it from memory. 

 

Key Features of OOP:

  1. Encapsulation: It refers to the bundling of data with the methods that operate on the data. It may also refer to the limiting of direct access to some of that data, such as an object’s components. Essentially, encapsulation prevents external code from being concerned with the internal workings of an object. 
  2. Abstraction: Showing only important details of an object to the external world/systems.  
  3. Inheritance: In OOP, classes can be created using pre-existing classes. Inheritance enables code reusability & extensibility. 
  4. Polymorphism: Polymorphism allows the use of the same name/operator for multiple functionalities depending on the class it has been called from. This allows different objects to have the same methods but different implementations.

 

Inheritance: A class can inherit properties & methods of another class. This is called Inheritance. Inheritance improves reusability.  

  • Single Inheritance: One class A inherits from another class B, i.e. A →  B
  • Multilevel Inheritance: A →  B →  C
  • Hierarchical Inheritance: Multiple classes inherit from the same base class A → B, A → C
  • Multiple Inheritance: A class can inherit from more than one class (though this isn’t supported in some languages). A → C, B → C

Access Control: The class creator can control how the properties and methods of the class/object are accessed. 

  • Public: The property can be accessed/modified, and the method can be called from the program using the class, by an inherited class, and by the class itself. 
  • Private: The property can be accessed/modified, and the method can be called only from the class.  Programs and inherited classes cannot access these. 
  • Protected: The property can be accessed/modified, and the method can be called from an inherited class and from the class itself. The program cannot access these. 

 

Interface: A Class containing methods with no implementation. An interface cannot be instantiated. 

Abstract Methods: Methods that contain no implementation. 

Abstract Class: A class containing at least one abstract method. Abstract classes cannot be instantiated, meaning, object of this class cannot be created. Abstract class can also contain concrete methods (with implementation), but must include atleast one abstract method. 

 

Types of Polymorphism:

  • Compile-time Polymorphism (Static binding):
      1. Method-overloading: Same class, same method name but different parameters. 
      2. The method to be executed is decided at compile-time itself. 
  • Run-time Polymorphism (Dynamic binding):
    1. Method-overriding: The same method name is used in the parent & child class. 
    2. The method to be executed is decided at run-time based on object type.