Motivation

My first ever coding interview made me realize how bad I was at OOP. This is one of my many efforts to reinforce my understanding about it.

Reminder

Python is an OOP language. Unlike procedure oriented programming which focuses on functions, OOP focuses on objects.

What are the four pillars of OOP?

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

One way to remember this is “A n E ven I nteresting P ie".

Abstraction (of Data)

  • Abstraction is the hiding/concealing of information. A process of displaying only necessary features
  • In a programming context, abstraction enables concealing the real implementation of an app and stressing the usage of the app.
  • Example: Think navigation to a more specific section of an app with just a tap. The user does not need to know how that is done.

Encapsulation

  • Encapsulates or wraps all related data into a single unit (usually a class).
  • Goal: Prevents direct modification of existing data from occurring, thus improving data security and maintenance.
  • Example: Think having all the relevant functions within a class for the purpose of opening Settings on your phone. In the future, any changes you want to make to Settings only need to be made within that class.

Inheritance

  • A method of creating a new class for using (inheriting) members of an already existing class. Multple inheritance is possible too i.e. inheriting attributes and methods from more than one class.
  • Goal: Improves code maintenance as no modification needs to be done to existing classes.
  • A simple example of inheritance can be seen here.

Polymorphism (Could be tricky)

  • Literally means: the ability to take various forms i.e. the same method name can do a different thing across different classes.
  • Allows defining methods in the child class with the same name as those in the parent class.
  • Advantage: You do not need to create methods with unique names for different classes. An example: Two classes can each have a method called calculate_area() and either of those methods can be called at will.

References

  1. Python Object Oriented Programming
  2. Four Pillars of Object Oriented Programming (OOP)
  3. Inheritance and Polymorphism in Python