Session Overview

  • Object Oriented Programming
  • Class
  • Object
  • Constructor
  • Inheritance

Object Oriented Programming (OOP)

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.
Why Use OOP?
  • Improves code organization.
  • Promotes code reuse.
  • Makes large applications easier to manage.
  • Widely used in Django development.

Class & Object

A Class is a blueprint and an Object is an instance created from that blueprint.

Example
class Student:

    def introduce(self):
        print("Hello Student")

s1 = Student()

s1.introduce()
Output
Hello Student
Understanding self

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 with Attributes
class Student:

    name = "Shree"
    city = "Mumbai"

s1 = Student()

print(s1.name)
print(s1.city)
Output
Shree
Mumbai

Note: Multiple objects can be created from the same class.

Constructor

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.

Example 1 : Constructor Execution
class Student:

    def __init__(self):
        print("Object Created")

s1 = Student()
Output
Object Created
Example 2 : Initialize Object Data
class Student:

    def __init__(self):
        self.name = "Shree"
        self.city = "Mumbai"

s1 = Student()

print(s1.name)
print(s1.city)
Output
Shree
Mumbai
Example 3 : Constructor with Parameters
class Student:

    def __init__(self, name, city):
        self.name = name
        self.city = city

s1 = Student("Shree", "Mumbai")

print(s1.name)
print(s1.city)
Output
Shree
Mumbai

Note: Constructors are commonly used to initialize object data when an object is created.

Constructor vs Normal Method

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

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.

Example
class Person:

    def introduce(self):
        print("I am a Person")

class Student(Person):
    pass

s1 = Student()

s1.introduce()
Output
I am a Person
Understanding pass

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.

Adding Child Class Method
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()
Output
I am a Person
Studying Python
Inheritance Diagram
Person
   ↑
Student

Note: Inheritance helps in code reuse because the child class can use features of the parent class.

Quick Revision

OOP
  • Uses Classes & Objects
  • Reusable Code
  • Structured Programs
Class
  • Blueprint
  • Defines Data
  • Defines Methods
Object
  • Instance of Class
  • Created from Class
  • Accesses Members
self
  • Current Object
  • Access Variables
  • Access Methods
Constructor
  • __init__()
  • Runs Automatically
  • Initialize Data
Inheritance
  • Code Reuse
  • Parent Class
  • Child Class

Assignment

  1. Create a class named Student and display a welcome message using a method.
  2. Create a class Employee with attributes Name and City and display their values using an object.
  3. Create a class Student with a constructor that initializes Name and Course.
  4. Create a class Person and a child class Student using inheritance. Call methods from both classes.
  5. Create a class Product with a constructor that accepts Product Name and Price and displays them.