Fundamentals of Object-Oriented Programming (OOP)
The following are the most fundamental reasons for adopting OOP:
-
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.
-
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.
-
Encapsulation
The data and the code associated with some type are
packaged together (encapsulated),
More on encapsulation.
-
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.
-
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:
-
SRP: The Single Responsibility Principle
Each function should do one task. Each class should handle one
sort of entity in the system.
-
OCP: The Open-Closed Principle
Classes should be open for extension, but closed for
modification.
-
LSP: The Liskov-Substitution Principle
We should always be able to use a sub-class in place of its
parent class.
-
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.
-
ISP: The Interface-Segregation Principle