ICT10 OOP CheatSheet
ICT10 OOP CheatSheet
ICT10 OOP CheatSheet
1. Class:
• A class is a blueprint for creating objects. It defines the attributes and methods that objects of
the class will have.
• Classes are defined using the class keyword.
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def drive(self):
print(f"Driving the {self.brand} {self.model}")
2. Object:
• An object is an instance of a class. It is created using the class blueprint and represents a
specific instance of that class.
• Objects encapsulate data and behavior.
3. Properties:
• Properties are the attributes or variables associated with an object. They represent the state of
the object.
print(car1.brand) # Output: Toyota
print(car2.model) # Output: Mustang
4. Methods:
• Methods are functions defined within a class that perform actions on the object or modify its
state.
• They define the behavior of the objects.
]
car1.drive() # Output: Driving the Toyota Camry
5. Constructor Function:
• The constructor function (__init__ method) initializes the object's state when it is created. It is
called automatically when an object is instantiated.
def __init__(self, brand, model):
self.brand = brand
self.model = model
6. Super Method:
• The super() method is used to call the constructor of the parent class within the child class. It
allows accessing the parent class's methods and properties.
class ElectricCar(Car):
def __init__(self, brand, model, battery_capacity):
super().__init__(brand, model)
self.battery_capacity = battery_capacity
7. Inheritance:
• Inheritance is a mechanism where a class inherits properties and methods from another class. It
promotes code reuse and enables the creation of a hierarchy of classes.
class ElectricCar(Car):
def __init__(self, brand, model, battery_capacity):
super().__init__(brand, model)
self.battery_capacity = battery_capacity
def charge(self):
print(f"Charging the {self.brand} {self.model} with {self.battery_capacity} kWh")
8. Class Attributes:
• Class attributes are attributes that are shared by all instances of a class. They are defined
outside the constructor.
class Circle:
pi = 3.14
def area(self):
return Circle.pi * self.radius ** 2
9. Instance Attributes:
• Instance attributes are attributes specific to each instance of a class. They are defined within
the constructor using self.
class Circle:
def __init__(self, radius):
self.radius = radius
10. Class Methods: - Class methods are methods that are bound to the class rather than the object
instance. They can access and modify class attributes.
class Dog:
num_dogs = 0
@classmethod
def display_num_dogs(cls):
print(f"Total number of dogs: {cls.num_dogs}")
11. Static Methods: - Static methods are methods that are not bound to the class or the object
instance. They are used when a method does not access or modify class or instance attributes.
class MathOperations:
@staticmethod
def add(x, y):
return x + y
12. Method Overriding: - Method overriding is the ability to define a method in a child class that has
the same name as a method in the parent class. The method in the child class overrides the method in
the parent class.
class Animal:
def make_sound(self):
print("Generic animal sound")
class Dog(Animal):
def make_sound(self):
print("Bark")
13. Multiple Inheritance: - Multiple inheritance is a feature in which a class can inherit properties
and methods from multiple parent classes.
class A:
def method_a(self):
print("Method A")
class B:
def method_b(self):
print("Method B")
class Dog(Animal):
def bark(self):
print("Dog barks")
dog = Dog()
dog.speak() # Output: Animal speaks
dog.bark() # Output: Dog barks
2. Multilevel Inheritance:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
class Labrador(Dog):
def color(self):
print("Labrador is brown")
labrador = Labrador()
labrador.speak() # Output: Animal speaks
labrador.bark() # Output: Dog barks
labrador.color() # Output: Labrador is brown
3. Hierarchical Inheritance:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
class Cat(Animal):
def meow(self):
print("Cat meows")
dog = Dog()
dog.speak() # Output: Animal speaks
dog.bark() # Output: Dog barks
cat = Cat()
cat.speak() # Output: Animal speaks
cat.meow() # Output: Cat meows
4. Multiple Inheritance:
class Father:
def skills(self):
print("Fishing, Hunting")
class Mother:
def skills(self):
print("Cooking, Sewing")
child = Child()
child.skills() # Output: Fishing, Hunting
# Output: Cooking, Sewing