18.1 Encapsulation
Encapsulation is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Account:
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
a = Account(100)
a.deposit(50)
print(a.balance)
Output
150
Explanation: This example demonstrates encapsulation in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.2 Public Members
Public Members is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Student:
def __init__(self, name):
self.name = name
s = Student("Sara")
print(s.name)
Output
Sara
Explanation: This example demonstrates public members in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.3 Protected Naming Conventions
Protected Naming Conventions is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Player:
def __init__(self):
self._score = 10
p = Player()
print(p._score)
Output
10
Explanation: This example demonstrates protected naming conventions in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.4 Private Name Mangling
Private Name Mangling is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Safe:
def __init__(self):
self.__code = 1234
def show_code(self):
return self.__code
s = Safe()
print(s.show_code())
Output
1234
Explanation: This example demonstrates private name mangling in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.5 Getters and Setters
Getters and Setters is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Person:
def __init__(self):
self._age = 0
def set_age(self, age):
self._age = age
def get_age(self):
return self._age
p = Person()
p.set_age(25)
print(p.get_age())
Output
25
Explanation: This example demonstrates getters and setters in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.6 Properties
Properties is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Temperature:
def __init__(self, celsius):
self._celsius = celsius
@property
def celsius(self):
return self._celsius
t = Temperature(20)
print(t.celsius)
Output
20
Explanation: This example demonstrates properties in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.7 The property() Function
The property() Function is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Product:
def __init__(self, price):
self._price = price
def get_price(self):
return self._price
price = property(get_price)
p = Product(15)
print(p.price)
Output
15
Explanation: This example demonstrates the property() function in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.8 Inheritance
Inheritance is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Animal:
def speak(self):
return "Sound"
class Dog(Animal):
pass
print(Dog().speak())
Output
Sound
Explanation: This example demonstrates inheritance in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.9 Single Inheritance
Single Inheritance is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Vehicle:
def move(self):
return "Moving"
class Car(Vehicle):
pass
print(Car().move())
Output
Moving
Explanation: This example demonstrates single inheritance in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.10 Multiple Inheritance
Multiple Inheritance is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Flyer:
def fly(self): return "Flying"
class Swimmer:
def swim(self): return "Swimming"
class Duck(Flyer, Swimmer): pass
d = Duck()
print(d.fly(), d.swim())
Output
Flying Swimming
Explanation: This example demonstrates multiple inheritance in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.11 Multilevel Inheritance
Multilevel Inheritance is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class A:
def show(self): return "A"
class B(A): pass
class C(B): pass
print(C().show())
Output
A
Explanation: This example demonstrates multilevel inheritance in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.12 Hierarchical Inheritance
Hierarchical Inheritance is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Parent:
def greet(self): return "Hello"
class Son(Parent): pass
class Daughter(Parent): pass
print(Son().greet())
print(Daughter().greet())
Output
Hello
Hello
Explanation: This example demonstrates hierarchical inheritance in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.13 Hybrid Inheritance
Hybrid Inheritance is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass
print(isinstance(D(), A))
Output
True
Explanation: This example demonstrates hybrid inheritance in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.14 Method Overriding
Method Overriding is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Animal:
def speak(self): return "Sound"
class Cat(Animal):
def speak(self): return "Meow"
print(Cat().speak())
Output
Meow
Explanation: This example demonstrates method overriding in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.15 The super() Function
The super() Function is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Parent:
def __init__(self): self.name = "Mina"
class Child(Parent):
def __init__(self): super().__init__()
print(Child().name)
Output
Mina
Explanation: This example demonstrates the super() function in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.16 Method Resolution Order
Method Resolution Order is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass
print([c.__name__ for c in D.mro()])
Output
['D', 'B', 'C', 'A', 'object']
Explanation: This example demonstrates method resolution order in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.17 Polymorphism
Polymorphism is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Dog:
def speak(self): return "Woof"
class Cat:
def speak(self): return "Meow"
for animal in [Dog(), Cat()]:
print(animal.speak())
Output
Woof
Meow
Explanation: This example demonstrates polymorphism in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.18 Duck Typing
Duck Typing is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Printer:
def print_item(self, item):
print(item)
class Report:
def __str__(self): return "Monthly report"
Printer().print_item(Report())
Output
Monthly report
Explanation: This example demonstrates duck typing in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.19 Abstract Classes
Abstract Classes is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
from abc import ABC
class Shape(ABC):
pass
print(issubclass(Shape, ABC))
Output
True
Explanation: This example demonstrates abstract classes in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.20 Abstract Methods
Abstract Methods is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self): pass
class Square(Shape):
def area(self): return 16
print(Square().area())
Output
16
Explanation: This example demonstrates abstract methods in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.21 Interfaces in Python
Interfaces in Python is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
from abc import ABC, abstractmethod
class Printable(ABC):
@abstractmethod
def print_data(self): pass
class Invoice(Printable):
def print_data(self): return "Invoice printed"
print(Invoice().print_data())
Output
Invoice printed
Explanation: This example demonstrates interfaces in python in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.22 Composition
Composition is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Engine:
def start(self): return "Engine started"
class Car:
def __init__(self): self.engine = Engine()
print(Car().engine.start())
Output
Engine started
Explanation: This example demonstrates composition in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.23 Aggregation
Aggregation is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Teacher:
def __init__(self, name): self.name = name
class Department:
def __init__(self, teacher): self.teacher = teacher
t = Teacher("Ali")
d = Department(t)
print(d.teacher.name)
Output
Ali
Explanation: This example demonstrates aggregation in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.24 Association
Association is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Doctor:
def help(self, patient): return "Helping " + patient.name
class Patient:
def __init__(self, name): self.name = name
print(Doctor().help(Patient("Sam")))
Output
Helping Sam
Explanation: This example demonstrates association in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.25 Mixins
Mixins is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class JsonMixin:
def to_dict(self): return self.__dict__
class User(JsonMixin):
def __init__(self, name): self.name = name
print(User("Lina").to_dict())
Output
{'name': 'Lina'}
Explanation: This example demonstrates mixins in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.26 SOLID Principles
SOLID Principles is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class TaxCalculator:
def calculate(self, amount): return amount * 0.13
print(TaxCalculator().calculate(100))
Output
13.0
Explanation: This example demonstrates solid principles in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.27 Chapter Practice Exercises
Chapter Practice Exercises is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Book:
def __init__(self, title): self.title = title
books = [Book("Python"), Book("OOP")]
for book in books:
print(book.title)
Output
Python
OOP
Explanation: This example demonstrates chapter practice exercises in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.
18.28 Chapter Mini Project
Chapter Mini Project is an important part of advanced object-oriented programming in Python. This topic helps you organize classes, control how objects work together, and create programs that are easier to reuse and maintain. The following beginner example shows the main idea in a small, readable form so you can study each line and understand the result.
Example
class Library:
def __init__(self): self.books = []
def add_book(self, title): self.books.append(title)
def show_books(self): return ", ".join(self.books)
library = Library()
library.add_book("Python Basics")
library.add_book("Advanced OOP")
print(library.show_books())
Output
Python Basics, Advanced OOP
Explanation: This example demonstrates chapter mini project in a simple program. Python runs the class definitions and statements from top to bottom, then displays the shown result. Study how the objects, methods, or relationships in the example produce the final output.