Object Oriented Programming (OOP) is a programming approach that organizes code using Classes and Objects.
OOP helps us write reusable, maintainable and structured programs.
| Concept | Description |
|---|---|
| Class | Blueprint used to create objects. |
| Object | Instance of a class. |
| Constructor | Initializes object data. |
| Inheritance | Allows one class to use features of another class. |
A Class is a blueprint and an Object is an instance created from that blueprint.
class Student:
def introduce(self):
print("Hello Student")
s1 = Student()
s1.introduce()
Hello Student
self refers to the current object of the class. It is used to access variables and methods belonging to that object.
class Student:
def introduce(self):
print("Hello Student")
s1 = Student()
s1.introduce()
When s1.introduce() is executed, Python automatically passes s1 as the value of self.
class Student:
name = "Shree"
city = "Mumbai"
s1 = Student()
print(s1.name)
print(s1.city)
Shree
Mumbai
Note: Multiple objects can be created from the same class.
A Constructor is a special method that is automatically called when an object is created.
In Python, the constructor is defined using the
__init__() method.
class Student:
def __init__(self):
print("Object Created")
s1 = Student()
Object Created
class Student:
def __init__(self):
self.name = "Shree"
self.city = "Mumbai"
s1 = Student()
print(s1.name)
print(s1.city)
Shree
Mumbai
class Student:
def __init__(self, name, city):
self.name = name
self.city = city
s1 = Student("Shree", "Mumbai")
print(s1.name)
print(s1.city)
Shree
Mumbai
Note: Constructors are commonly used to initialize object data when an object is created.
In the previous examples, we created objects without using a constructor. We simply created methods and called them manually.
class Student:
def introduce(self):
print("Hello Student")
s1 = Student()
s1.introduce()
Here, the introduce() method executes only when we call it explicitly.
A Constructor is different because it runs automatically when an object is created.
class Student:
def __init__(self):
print("Object Created")
s1 = Student()
When s1 = Student() is executed, the __init__() constructor runs automatically.
Inheritance allows one class to use the properties and methods of another class.
The existing class is called the Parent Class and the new class is called the Child Class.
class Person:
def introduce(self):
print("I am a Person")
class Student(Person):
pass
s1 = Student()
s1.introduce()
I am a Person
The pass statement is used when we do not want to write any code inside a class, function or loop.
It acts as a placeholder and prevents syntax errors.
class Student:
pass
Here, the Student class is empty, so pass is used as a placeholder.
class Person:
def introduce(self):
print("I am a Person")
class Student(Person):
def study(self):
print("Studying Python")
s1 = Student()
s1.introduce()
s1.study()
I am a Person
Studying Python
Person
↑
Student
Note: Inheritance helps in code reuse because the child class can use features of the parent class.