26
Lesson 22 of 44

Object-oriented programming

OOP (Object-Oriented Programming) is a fundamental approach in programming that is based on the creation and manipulation of objects to effectively and efficiently solve problems. This lesson provides you with the essential skills to work with object-oriented programming and address problems in a structured and modular way. Enhance your programming skills through practical exercises and master the principles of object-oriented programming.

  • 11 theory articles
  • 15 practice exercises
Start lesson

Fundamentals concepts 11

Classes and objects

Python is an object-oriented language. This paradigm organizes programs around two fundamental concepts: classes and objects.

Theory
Read more

Attributes and Methods

The attributes are the variables that store the data of an object, and the methods are the functions that define what the object…

Theory
Read more

Constructors

A constructor is the special method that runs automatically when an object is created, and it is used to give its attributes…

Theory
Read more

Access control: public, protected and private

Many languages have keywords such as public or private to control who can use an attribute. Python has no such keywords: it…

Theory
Read more

Getters, setters and properties

Getters and setters are methods used to read and modify the attributes of an object in a controlled way. For example, to validate…

Theory
Read more

Inheritance

Inheritance lets you create a new class from one that already exists. The new class (the subclass ) receives all the attributes…

Theory
Read more

Polymorphism and class diagrams

Polymorphism means "many forms": objects of different classes respond to the same message (the same method), each in its own way.

Theory
Read more

Abstract classes and interfaces

Sometimes you want a class that serves only as a template : it defines which methods its subclasses must have, but it makes no…

Theory
Read more

Special methods, class attributes and composition

This article brings together three tools that complete a class: special methods (with double underscores), attributes shared by…

Theory
Read more

Class attributes, class methods and static methods

So far everything your objects store belongs to each object . But sometimes there is data or functions that belong to the class…

Theory
Read more

Multiple inheritance, MRO and composition

Inheritance is a powerful tool that can be misused. This article covers the advanced cases and when another option is better.

Theory
Read more

Practice exercises 15

Person class and method ToString()

Create a Python program that asks the user for three names and stores them in a list of objects of the Person class.

Exercise Medium
Solve it

Constructors and destructors

Create a Python program that asks the user for three names and stores them in a list of objects of the Person class.

Exercise Medium
Solve it

Class person, student and teacher

Develop a Python program made of three classes: Person , Student and Teacher . The Student and Teacher classes inherit from the…

Exercise Medium
Solve it

Photo book class

Develop a Python program to manage a photo album using object-oriented programming.

Exercise Medium
Solve it

Creating Classes and Objects

Create a class called Person with the attributes name (text), age (integer) and address (text).

Exercise Easy
Solve it

Inheritance of objects

Develop a Python program that asks the user for three names and stores them in a list of Person objects. Two of them are students…

Exercise Medium
Solve it

Shapes - Class diagram

Create a Python program that models several geometric shapes from the following class diagram. The diagram represents public…

Exercise Medium
Solve it

Interfaces

Develop a Python program that implements an interface called IVehicle . In Python an interface is an abstract class ( abc.ABC )…

Exercise Medium
Solve it

Abstract classes

Create a Python program with an abstract class Animal that has a name and three methods: set_name(name) , get_name() and eat() …

Exercise Medium
Solve it

Bank account with exceptions

A class protects its data: the balance only changes through methods that check that the operation is valid and, if it is not…

Exercise Medium
Solve it

Instance counter

Instance attributes are different in each object; class attributes are shared by all of them. A @classmethod works with the class…

Exercise Medium
Solve it

Vectors with operators

With special methods ( __add__ , __sub__ , __eq__ …) your objects can use Python operators as if they were numbers.

Exercise Medium
Solve it

Iterable playlist

If a class defines __iter__ , __len__ and __contains__ , its objects behave like collections: they are traversed with for …

Exercise Medium
Solve it

Employee hierarchy

Inheritance lets a subclass reuse a base class and specialize it, and polymorphism makes the same method behave differently…

Exercise Medium
Solve it

Stack as a class

A stack (last in, first out) is a good example of an abstract data type : the class hides how it stores the elements and offers…

Exercise Medium
Solve it