π Python OOP Interview Cheat Sheet
Absolutely π I analyzed the uploaded notes and organized them into a Python OOP Interview Cheat Sheet. I’ve kept it focused on the concepts covered in your material: class, object, __init__, self, the four pillars, inheritance types, polymorphism, and constructor overloading.
π Python OOP Interview Cheat Sheet
1. OOP
OOP = Object-Oriented Programming
OOP organizes a program using classes and objects.
Real-life example
Class → Car Object → BMW car
A car has:
- Properties: color, brand, speed
- Methods: start(), stop(), drive()
2. Class
Interview definition
A class is a blueprint or template for creating objects.
class Car: pass
Remember:
Class = Blueprint
3. Object
Interview definition
An object is an instance of a class.
class Car: pass car1 = Car() car2 = Car()
Car → Class car1 → Object car2 → Object
Remember:
Class = Blueprint
Object = Real thing
4. __init__() — Constructor
Interview definition
__init__()is a constructor that runs automatically when an object is created.
class Student: def __init__(self, name, age): self.name = name self.age = age student = Student("Rahul", 20)
Here:
name → Rahul age → 20
Remember:
__init__()→ Constructor → Runs when object is created
5. self
Most important interview question ⭐
What is self?
selfrefers to the current object.
class Car: def __init__(self, color, brand): self.color = color self.brand = brand def drive(self): print(self.brand, "is driving") car1 = Car("Red", "BMW") car2 = Car("Blue", "Audi") car1.drive() car2.drive()
Output:
BMW is driving Audi is driving
Think:
car1 → self car2 → self
So:
car1.drive()
approximately means:
drive(car1)
Easy trick
self= current object / "my"
6. Attributes vs Methods
class Car: def __init__(self, color, brand): self.color = color self.brand = brand def drive(self): print("Driving")
| Term | Example |
|---|---|
| Attribute | color, brand |
| Method | drive() |
Attribute = Data
Method = Action
7. Four Pillars of OOP ⭐⭐⭐
Remember:
E → I → P → A
E → Encapsulation I → Inheritance P → Polymorphism A → Abstraction
8. Encapsulation
Interview definition
Encapsulation means keeping data and methods together inside a class and controlling access to the data.
Example:
class BankAccount: def __init__(self, balance): self.__balance = balance def deposit(self, amount): self.__balance += amount def get_balance(self): return self.__balance
__balance → hidden/private data deposit() → modifies data get_balance() → accesses data
Remember
Encapsulation = Data hiding + Protection
9. Inheritance
Interview definition
Inheritance allows a child class to reuse properties and methods of a parent class.
class Animal: def eat(self): print("Eating") class Dog(Animal): def bark(self): print("Barking") dog = Dog() dog.eat() dog.bark()
Dog gets eat() from Animal.
Animal ↓ Dog
Remember
Inheritance = Code Reusability
10. Types of Inheritance ⭐
There are 5 common types:
1. Single
A ↓ B
One parent → One child.
2. Multiple
A ──┐ ↓ C ↑ B ──┘
Multiple parents → One child.
class Child(Father, Mother): pass
3. Multilevel
Grandfather ↓ Father ↓ Son
4. Hierarchical
Animal / \ Dog Cat
One parent → Multiple children.
5. Hybrid
Combination of multiple inheritance types.
A / \ B C \ / D
11. Polymorphism ⭐⭐⭐
Interview definition
Polymorphism means one name, many forms.
Same method name → Different behavior.
class Dog: def sound(self): print("Woof") class Cat: def sound(self): print("Meow") dog = Dog() cat = Cat() dog.sound() cat.sound()
Output:
Woof Meow
Both have:
sound()
but behave differently.
12. Compile-Time vs Run-Time Polymorphism ⭐⭐⭐
| Compile-Time | Run-Time | |
|---|---|---|
| Decision | During compilation | During execution |
| Common example | Method Overloading | Method Overriding |
| Python | No traditional support | Yes |
| Main concept | Overloading | Overriding |
Compile-Time
Usually associated with method overloading.
Example concept:
add(a, b) add(a, b, c)
Python doesn't support traditional method overloading.
Instead, use default arguments:
class Calculator: def add(self, a, b, c=0): return a + b + c c = Calculator() print(c.add(10, 20)) print(c.add(10, 20, 30))
Output:
30 60
13. Run-Time Polymorphism
Usually achieved through method overriding.
class Animal: def sound(self): print("Animal sound") class Dog(Animal): def sound(self): print("Woof") class Cat(Animal): def sound(self): print("Meow") animals = [Dog(), Cat()] for animal in animals: animal.sound()
Output:
Woof Meow
Python determines which sound() to call at runtime.
Remember
Runtime Polymorphism = Method Overriding
14. Duck Typing
Python also supports polymorphism through duck typing.
Simple definition
Python cares about what an object can do, rather than its exact type.
class Dog: def sound(self): print("Woof") class Cat: def sound(self): print("Meow") def make_sound(animal): animal.sound() make_sound(Dog()) make_sound(Cat())
The function only cares that the object has:
sound()
15. Abstraction
Interview definition
Abstraction means hiding implementation details and showing only necessary functionality.
Example:
from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass class Dog(Animal): def sound(self): print("Woof") dog = Dog() dog.sound()
Here Animal defines what should exist:
sound()
but Dog provides the actual implementation.
Remember
Abstraction = Hide implementation + Show important functionality
16. Can we create multiple __init__()?
❌ No, not traditional constructor overloading.
class Car: def __init__(self, color): self.color = color def __init__(self, color, brand): self.color = color self.brand = brand
The second __init__() replaces the first one.
✅ Use default arguments
class Car: def __init__(self, color, brand=None): self.color = color self.brand = brand car1 = Car("Red") car2 = Car("Blue", "BMW")
π₯ Top Interview Questions
Basic
Q1. What is OOP?
→ Object-Oriented Programming.
Q2. What is a class?
→ A blueprint/template for creating objects.
Q3. What is an object?
→ An instance of a class.
Q4. What is __init__()?
→ Constructor that runs when an object is created.
Q5. What is self?
→ Reference to the current object.
Four Pillars
Q6. What are the four pillars of OOP?
Encapsulation, Inheritance, Polymorphism, Abstraction.
Q7. What is encapsulation?
Keeping data and methods together and controlling access to data.
Q8. What is inheritance?
Reusing properties and methods from a parent class.
Q9. What is polymorphism?
Same interface/method name with different behavior.
Q10. What is abstraction?
Hiding implementation details and exposing necessary functionality.
Polymorphism
Q11. What is method overriding?
When a child class provides its own implementation of a parent method.
Q12. Does Python support method overloading?
Python does not support traditional method overloading. Default arguments or
*argscan be used to achieve similar behavior.
Q13. What is runtime polymorphism?
Polymorphism where the method implementation is determined at runtime, commonly through method overriding.
Q14. What is duck typing?
Python determines compatibility based on what an object can do rather than its exact type.
π§ 30-Second Revision
OOP │ ├── Class → Blueprint ├── Object → Instance ├── __init__ → Constructor ├── self → Current object │ └── 4 Pillars │ ├── Encapsulation → Protect data ├── Inheritance → Reuse code ├── Polymorphism → Different behavior └── Abstraction → Hide details
⭐ Memorize this for interview:
Class = Blueprint
Object = Instance
__init__= Constructor
self= Current Object
Encapsulation = Protect
Inheritance = Reuse
Polymorphism = Many Forms
Abstraction = Hide Details
Comments
Post a Comment