• Python Snacks
  • Posts
  • Part 2: Mastering Object Oriented Programming: The 4 Principals of OOP

Part 2: Mastering Object Oriented Programming: The 4 Principals of OOP

In this multi-part series, I'm explaining the basics of object oriented programming (OOP) in Python.

This is part 2 of a 3 part series on object oriented programming. Below are all 3 parts of this series:

One of the great things about code is that there are many ways to organize it. This can be done in several different ways through functional programing, procedural programming, modular, and so forth.

Object oriented programming (OOP) is just another way to organize code. Through OOP, we’re able to structure these real-world concepts as “objects” that contain attributes and methods.

Here, I’m going to continue using the pen as an example to explain the 4 principals. If you need a reference to the code, here’s the direct link to the code.

Principal #1: Encapsulation

The first principal of object oriented programming is encapsulation. Our pen has multiple parts: the ink, grip, and a cap. All of these are bundled into one physical object: the pen. However, we don’t need to know how the ink flows through the pen or how the spring mechanism inside the pen works.

This is what encapsulation in object oriented programing is - it’s bundling the attributes and functionality into a single unit. You’re able to get into the pen itself (such as the ink reservoir) through methods such as click() or write(), while some other details are hidden, such as the spring mechanism.

Principal #2: Abstraction

The second principal of object oriented programming is abstraction. When you write with the pen, you don’t really care about who manufactured it or the engineering process behind the retractable. You really only care if you’re able to write or not.

The same can be said with objects - abstraction means we’re exposing only what is necessary, while hiding the complex inner workings on how that behavior is implemented.

In our pen class, users don’t need to know how the ink flows - they just know if there is ink and if they’re able to call the write() method or not.

Principal #3: Inheritance

The third principal of object oriented programming is inheritance. If we stick with our pen example, it won’t be clear as to how inheritance works, so we’re going to pivot slightly for this example. Instead, we’ll use vehicles.

Vehicles have wheels, an engine, and the ability to move. Under this vehicle category, we may have cars, bikes, and trucks. While they all have wheels, an engine, and the ability to move, they’re each unique on how these are implemented with potentially unique features.

The same can be said about inheritance - you can have “common code” that gets passed down.

Principal #4: Polymorphism

The final principal of object oriented programming is polymorphism. To me, this was the most difficult principal to grasp. Using our pen example, we can use any pen to write - it doesn’t matter if it’s ballpoint or gel.

You don’t need to know the exact type of pen to perform the action of writing - you just expect it to work, even though the writing experience may be different depending upon the type of the pen.

This is what polymorphism is - it allows us to interact with different objects in the same way. We can call the write() method on a BallpointPen class or a GelPen class even though the write() method may be implemented slightly differently.

The idea is that multiple types of objects can be used interchangeably, and the exact behavior will depend on the object’s specific class.

The 4 Principals of Object Oriented Programming: A Summary

To recap:

  • Encapsulation: Involves organizing code so that an object’s data and behavior are kept together, exposing only what’s necessary and keeping internal details hidden.

    Abstraction: Simplifies interaction by allowing access to essential features, while concealing the complexities behind how they are implemented.

    Inheritance: Allows new objects to be built from existing ones, sharing common traits while adding unique features or behaviors.

    Polymorphism: Enables objects to be used interchangeably, regardless of their specific types, ensuring they behave appropriately based on their underlying class.

Happy coding!

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

or to participate.