2) Classes, Objects, and Methods Lesson

Python OOP - Main Concepts

10 min to complete · By Martin Breuss

The four primary traits of Object-Oriented Programming (OOP) are:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Four Traits of OOP

In this lesson, you'll get a quick theoretical overview of each of them. Don't worry if you don't quite grasp each concept yet. You'll train everything with practical examples throughout this section of the course.

1. Abstraction

When you apply abstraction, you reduce information to the essential concepts. For example, you can think of this image as an abstraction of a sunset over water:

Photo by https://unsplash.com/@gradienta Gradienta on Unsplash

In the context of OOP, abstraction means that you create a black box when writing your code. This means that your users won't need to know about all the contents and capacities of your implementation, but they'll still be able to use it.

As a real-world example, you can think of how you're able to use a phone without needing to know how all the electronics are connected internally. The phone provides you with a high-level abstracted interface that usually has only a couple of buttons yet allows you to interact with all its functionality.

Python's basic unit of abstraction is the class. A class defines the form of an object by specifying both the data and the code that operates on the data.

2. Encapsulation

Encapsulation means that you're binding your data and the code that changes it together. This allows you to keep your data safe from unintended manipulation. You'll model your code in a way that it has a public interface that exposes the data you want to be accessed, as well as methods to work with it. You'll also create a private interface that indicates data and methods that shouldn't be modified by the user. When code and data are linked together like this, you can speak of an object.

Sign saying 'Private', This area is private! - Photo by https://unsplash.com/@timmossholder Tim Mossholder on Unsplash
Colorful illustration of a light bulb

Info: Python doesn't support strong encapsulation like some other OOP languages do. A private interface won't be completely private and is so only by convention. You'll learn more about what that means in the upcoming lessons.

When working over the practical examples, you'll see that you can think of a Python object pretty much as you'd think about any real-world object. But more about this in just a bit.

3. Inheritance

Inheritance allows one object to access the properties of its parent object. If you define a class that inherits from another class, then an object of the child class will be everything that the parent class is and can do everything that the parent class can do.

For example, you can think of an apple as being classified as an Apple object. Apples are fruits, so they could inherit from a Fruit class. If all fruits in your Fruit class are defined as "a product of plant growth", then your Apple that inherits from Fruit, would automatically also be "a product of plant growth".

Five apples - Photo by https://unsplash.com/@stepanbabanin Stepan Babanin on Unsplash

If you define that every Fruit is edible, then each Apple that inherits from Fruit will be edible as well.

4. Polymorphism

The term polymorphism stands for "having many forms". In OOP, polymorphism means that objects that belong to different classes can respond to the same message. However, they can respond in different ways. Usually, this comes in combination with inheritance, where a parent class will define a method, and different child classes will handle what the method does in different ways.

For example, you could have two objects, Dog and Cat, that both inherit from an Animal class. They can both have a method .greet() that was initially defined in the parent class. Both dogs and cats can greet others, but they do it in distinctly different ways. Therefore, calling .greet() on a Dog object will have a different effect than calling .greet() on a Cat object.

Dog on a beach lifting one paw,

You could model this in OOP by defining an empty .greet() method in your Animal class and overwriting the method with different functionality in both the Dog and the Cat classes.

Soon, you'll get to know some reasons why modeling a custom object can be more helpful than using existing Python data structures.

5. Composition

Composition is very similar to the idea of inheritance since its function is connecting functionality between classes. Composition, however, focuses on creating smaller objects or components that are defined through what they have rather than what they are, which allows for larger objects to build from smaller ones.

This differs from inheritance since inheritance can often have a several-tiered chain of inheritance where no individual subclass can truly be independently used.

You will dive deeper into the difference between these two concepts in the upcoming lessons.

Colorful illustration of a light bulb

Additional Resources

Summary: Python OOP Main Concepts

OOP paradigm builds on top of four fundamental concepts

OOP Paradigm

  1. Abstraction: creates a black box that shows only what's necessary in a user-friendly interface.
  2. Encapsulation: binds data and logic together, exposing a public interface and hiding other parts in a private interface.
  3. Inheritance: allows classes to pass on their attributes and methods to child classes.
  4. Polymorphism: gives the possibility for different classes to implement the same attributes and methods but handle their logic differently.
  5. Composition: allows you to compose an object ("composite") of one or more smaller objects ("components"), each of which represents a part of the whole. Composition allows an object to be defined through what it has rather than what it is, and it allows larger objects to be built from smaller objects.