A.3: Object-Oriented Programming
- 2.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
- 3.OOP helps us group related data and functions together, potentially simplifying code logic
Usercould be 1 such class, where a user could have multiple attributes such as name, email, password, and also multiple "methods" (another word for functions that are part of a class) that perform functionality on specific user "instances". An "instance" is 1 instantiation of a class, e.g. a
Userinstance that represents Kai. 1 example of a method on the
Userclass could be
validatePassword, which might hash an input password and verify if it matches the user's hashed password in the database.
We have already used classes extensively in Coding Bootcamp, perhaps unknowingly. For example, the
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.
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
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.
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
greetingmethod could produce different results between instances of