Introduction to OOP
What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm that organizes code around objects rather than functions and logic. Objects combine data (attributes) and behavior (methods) into a single unit.
Think of it like modeling real-world things:
- A Car has attributes (color, brand, speed) and behaviors (accelerate, brake, turn)
- A BankAccount has attributes (balance, owner) and behaviors (deposit, withdraw)
- A Student has attributes (name, grade) and behaviors (study, take_exam)
Why Use OOP?
OOP provides several key benefits:
- Organization: Group related data and functions together
- Reusability: Create templates (classes) that can be reused
- Maintainability: Changes to one class don't affect others
- Abstraction: Hide complexity behind simple interfaces
Classes vs Objects
Understanding the difference is crucial:
• breed
• age
• fetch()
breed = "Golden"
age = 3
breed = "Husky"
age = 5
Class: A blueprint or template (defines what a Dog is)
Object: A specific instance of a class (an actual dog with real values)
A class is like a cookie cutter - it defines the shape. An object is like a cookie - it's the actual thing created from the template. You can make many cookies (objects) from one cutter (class).
Run this code to see the difference between a class and its objects.
Dog is the class (template), while buddy and max are objects (instances). Each has its own name and breed.
Classes & Objects
Defining a Class
Use the class keyword to define a class. Class names use PascalCase by convention:
The __init__ Method
The __init__ method is a special method called a constructor. It's automatically called when you create a new object:
self refers to the current instance of the class. It's how the object accesses its own attributes and methods. Always include self as the first parameter in instance methods.
Creating and Using Objects
Create a Person class with name and age attributes. Create two Person objects and print their information.
class Person:, add __init__(self, name, age), and assign self.name = name and self.age = age.
Methods & self
Instance Methods
Methods are functions defined inside a class. They define the behaviors of objects:
Calling Methods
Call methods on objects using dot notation:
Forgetting self is the #1 OOP mistake! Every instance method must have self as its first parameter, and use self.attribute to access instance data.
Methods that Return Values vs Modify State
Methods can either return information or modify the object's state:
Create a Rectangle class with methods to calculate area and perimeter. Then test it with different rectangles.
self and returns the calculated value.
Inheritance
What is Inheritance?
Inheritance allows a class to inherit attributes and methods from another class. This promotes code reuse and creates logical hierarchies:
• age
• sleep()
+ fetch()
+ purr()
Creating Child Classes
Put the parent class name in parentheses to inherit from it:
super() gives you access to the parent class. Use super().__init__() in child classes to run the parent's constructor first, then add your own initialization.
Method Overriding (Polymorphism)
Child classes can override parent methods to provide specialized behavior:
Create a Vehicle parent class and Car and Motorcycle child classes. Each should have a describe() method.
class Car(Vehicle): to inherit. In __init__, call super().__init__(brand, year) first, then add the child-specific attribute.
Encapsulation & Special Methods
Encapsulation
Encapsulation means bundling data with the methods that operate on it, and restricting direct access to some components. In Python, we use naming conventions:
• public - accessible anywhere
• _protected - "please don't access directly" (convention)
• __private - name mangled to _ClassName__attr
Special Methods (Dunder Methods)
Python has special methods with double underscores that customize class behavior:
Special methods let your classes work with Python's built-in functions. __str__ works with print(), __len__ works with len(), __eq__ works with ==.
Create a Point class with __str__, __eq__, and a method to calculate distance from origin.
math.sqrt() and **2 for squaring. For __eq__, compare both x and y coordinates.
Chapter Summary
What You've Learned
Congratulations! You've completed the final chapter. Here's what you've mastered:
- OOP Fundamentals: Classes vs objects, why OOP matters
- Classes & Objects:
classkeyword,__init__, creating instances - Methods & self: Instance methods, the purpose of
self - Inheritance: Parent/child classes,
super(), method overriding - Encapsulation: Public, protected, private attributes
- Special Methods:
__str__,__repr__,__eq__,__len__
OOP Quick Reference
1. Encapsulation: Bundle data and methods, control access
2. Abstraction: Hide complexity, expose simple interfaces
3. Inheritance: Create new classes based on existing ones
4. Polymorphism: Same interface, different behaviors
Python Course Complete!
Ready for the Final Exam?
Put all your OOP skills to the test with 3 challenging coding problems. Hard difficulty.
Take Chapter 8 Exam →