Fundamentals of Object-Oriented Programming (OOP)

The following are the most fundamental reasons for adopting OOP:

  1. Abstraction
    We can hide the particulars of the implementation of some type behind the public interface to a class. In C++ we can abstract even further using templates.
    More on abstraction.
  2. Inheritance
    We can create a hierarchy of classes, in which the methods of an "ancestor" class are inherited by a "descendant" class, unless they are over-ridden.
    More on inheritance.
  3. Encapsulation
    The data and the code associated with some type are packaged together (encapsulated),
    More on encapsulation.
  4. Data Hiding
    The data members of a class can be hidden from view outside the class, so that they can only be accessed and altered through the public interface of the class. This property is closely associated with abstraction and encapsulation. (We need encapsulation to achieve data hiding, and it is partly through data hiding that we achieve abstraction.)
    More on data hiding.
  5. Polymorphism
    Given that a function has a parameter of type A, it can also accept as an argument any instance of a class descended from A. Furthermore, when it calls some method on A, the appropriate method of the descendant class will be called.
    More on polymorphism.

And here are some crucial OOP design principles:

  1. SRP: The Single Responsibility Principle
    Each function should do one task. Each class should handle one sort of entity in the system.
  2. OCP: The Open-Closed Principle
    Classes should be open for extension, but closed for modification.
  3. LSP: The Liskov-Substitution Principle
    We should always be able to use a sub-class in place of its parent class.
  4. DIP: The Dependency-Inversion Principle
    General classes should not depend upon more specific class. Specific classes should depend on general classes. Even better, both should depend on abstract classes.
  5. ISP: The Interface-Segregation Principle