We will use OOP to model non-built-in data structures such as linked lists, trees, graphs and heaps with relevant classes for each data structure
OOP helps us group related data and functions together, potentially simplifying code logic
In the algorithm context, we will use OOP to work with non-built-in data structures to help us solve problems more efficiently. These new data structures include linked lists, trees, graphs and heaps.
4 Benefits of OOP
The following knowledge is for context and not strictly necessary for solving algorithm problems.
Below are the 4 primary benefits of OOP to help us get a sense of why OOP can be helpful.
Visual explanation of the 4 benefits of OOP. Source: Coding with Mosh
1) Encapsulation: Data and methods together
One of the primary benefits of classes is to "encapsulate" data into a class instance, and use class methods to manipulate that data. Without OOP we would write code "procedurally" or "functionally", resulting in more variables to keep track of, and more context to pass into each function.
2) Abstraction: Simplify interfaces
We can think of classes as libraries that we import: We do not need to know how library functions are implemented, we only need to know what they do and how to use them.
Inheritance is a way to re-use code such that instead of multiple classes re-using the same or similar functionality, we can have 1 base class that implements the shared functionality and "child" or "inherited" classes that "inherit" the base class' functionality before adding their own.
Polymorphism means the same method can be implemented differently for different classes. For example, a hypothetical greeting method could produce different results between instances of AmericanPerson and ChinesePerson classes.