OOP helps organize code by grouping related data and actions together. It makes programs easier to build, understand, and change.
OOP principles overview in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
class ClassName: def __init__(self, value): self.attribute = value def method(self): # action code here
class defines a new type or blueprint.
__init__ is a special method to set up new objects.
Examples
Python
class Dog: def __init__(self, name): self.name = name def bark(self): print(f"{self.name} says Woof!")
Python
class Animal: def __init__(self, species): self.species = species class Cat(Animal): def meow(self): print("Meow!")
Sample Program
This program creates a Person with a name and age, then says hello.
Python
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): print(f"Hello, my name is {self.name} and I am {self.age} years old.") # Create a person object p = Person("Alice", 30) p.greet()
Important Notes
Encapsulation means keeping data safe inside objects.
Inheritance lets one class get features from another.
Polymorphism means different classes can use the same method name in their own way.
Abstraction hides complex details and shows only what is needed.
Summary
OOP groups data and actions into classes and objects.
Four main principles: Encapsulation, Inheritance, Polymorphism, Abstraction.
OOP helps write clear, reusable, and easy-to-change code.
Practice
1. Which of the following is NOT one of the four main principles of Object-Oriented Programming (OOP)?
easy
Solution
Step 1: Recall the four main OOP principles
The four main principles are Encapsulation, Inheritance, Polymorphism, and Abstraction.Step 2: Identify the option not in the list
Compilation is a process related to converting code, not an OOP principle.Final Answer:
Compilation -> Option AQuick Check:
OOP principles exclude Compilation [OK]
Hint: Remember OOP principles: E, I, P, A [OK]
Common Mistakes:
- Confusing compilation with OOP concepts
- Mixing up abstraction with compilation
- Thinking all programming terms are OOP principles
2. Which Python keyword is used to create a new class?
easy
Solution
Step 1: Recall Python syntax for defining classes
In Python, the keywordclassis used to define a new class.Step 2: Check other options
defdefines functions,objectis a base class, andfuncis not a Python keyword.Final Answer:
class -> Option DQuick Check:
Use 'class' to define classes [OK]
Hint: Classes start with 'class' keyword in Python [OK]
Common Mistakes:
- Using def instead of class for classes
- Confusing object with class keyword
- Trying to use func which is invalid
3. What will be the output of this code?
class Animal:
def speak(self):
return "Sound"
class Dog(Animal):
def speak(self):
return "Bark"
pet = Dog()
print(pet.speak())medium
Solution
Step 1: Understand inheritance and method overriding
Dog class inherits from Animal and overrides the speak method to return "Bark".Step 2: Check which speak method is called
pet is an instance of Dog, so pet.speak() calls Dog's speak method, returning "Bark".Final Answer:
Bark -> Option AQuick Check:
Overridden method returns 'Bark' [OK]
Hint: Child class method overrides parent method [OK]
Common Mistakes:
- Expecting parent class method output
- Confusing method overriding with overloading
- Thinking print outputs None
4. Find the error in this code snippet:
class Car:
def __init__(self, model):
self.model = model
def display(self):
print(Model)medium
Solution
Step 1: Check the print statement inside display method
The print statement usesModelwhich is undefined; it should useself.modelto access the instance variable.Step 2: Verify other parts
The constructor name__init__is correct, and method has self parameter. Class name capitalization is fine.Final Answer:
Model should be self.model in print -> Option CQuick Check:
Use self to access instance variables [OK]
Hint: Use self.variable to access instance data [OK]
Common Mistakes:
- Forgetting self in method parameters
- Using variable name without self prefix
- Thinking constructor name is incorrect
5. You want to create a class that hides its internal data and only allows access through methods. Which OOP principle does this demonstrate?
hard
Solution
Step 1: Understand the principle of hiding data
Hiding internal data and controlling access through methods is called Encapsulation.Step 2: Differentiate from other principles
Inheritance is about reusing code, Polymorphism is about using methods in different ways, Abstraction is about hiding complexity but not necessarily data.Final Answer:
Encapsulation -> Option BQuick Check:
Data hiding = Encapsulation [OK]
Hint: Data hiding means Encapsulation [OK]
Common Mistakes:
- Confusing encapsulation with abstraction
- Mixing inheritance with data hiding
- Thinking polymorphism hides data
