Object-Oriented Programming (OOP) is built upon four fundamental concepts, often called the four pillars of OOP. These principles help in organizing code, reducing complexity, and making it more reusable:
Inheritance allows a class (child class) to inherit attributes and methods from another class (parent class).
Teacher class that inherits from Person and add a subject attribute.greet() method in the Teacher class to include the subject they teach (e.g., "Hi, I'm [name] and I teach [subject].").Staff class that inherits from Person and adds a department attribute.Student, Teacher, and Staff, and call the greet() method on each to see the different behaviors.Abstraction is about hiding the complex reality while exposing only the necessary parts. In Python, we achieve this using Abstract Base Classes (ABC). An abstract class cannot be instantiated directly; it serves as a template that enforces subclasses to implement specific methods.
Square subclass of Shape that implements the area() method (remember to define side in the constructor).perimeter() to the Shape class and implement it in both Circle and Square classes.Polymorphism allows different classes to be treated as instances of the same general class through the same interface. The most common use is when multiple classes have the same method names but different behaviors.
The following example shows how Circle and Rectangle both implement the area() method defined in the Shape interface. Even though the calculation is different, we can call area() on any object that is a Shape.
area() function on them for comparison.Animal with an abstract method make_sound().Dog and Cat that implement make_sound() differently.animal_chorus(animals) that takes a list of animal instances and calls make_sound() on each. Demonstrate polymorphism by passing a list containing both a Dog and a Cat.Encapsulation is the practice of hiding the internal state of an object and requiring all interaction to be performed through an object's methods.
In Python, encapsulation is mostly a convention:
_attribute indicates that the attribute is protected. This is a hint that it should not be used on instances directly from outside; it is intended for use inside the class definition and by its subclasses.__attribute indicates that the attribute is private. It is more restricted than protected because Python uses name mangling to hide it even from subclasses.Situations for Use: Use protected when you want to share data with child classes but hide it from the end-user. Use private when you want to ensure the data is strictly local to the parent class and cannot be accidentally overridden or accessed by subclasses.
The following example demonstrates that while a subclass can access a protected attribute (_year), it cannot directly access a private attribute (__citizenship) from the parent class.
BankAccount class with a private __balance. Implement deposit(amount) and get_balance() methods. Ensure the balance cannot be modified directly from outside.withdraw(amount) method that only deducts funds if the balance is sufficient.+48 790-430-860
analysislessons@gmail.com