Introduction to Class and Object

Concept of OOP

  • Provides a means of structuring programs so that properties and behaviors are bundles into individual objects.
  • OOP reflects the real world behavior of how things work
  • It make visualization easier because it is closest to real world scenarios.
  • We can reuse the code through inheritance, this saves time, and shrinks our project.
  • There are flexibility throug polymorphism

Class

  • A class is the blueprint for the objects created from that class
  • Each class contains some data definitions(called fields), together with methods to manipulate that data.
  • when the object is instantiated from the class, an instance variable is created for each filed in the class.

Object

  • Object are basic run time entities in an object oriented system.
  • It is an instance of class
  • we can say that objects are the variables of the type class.

Class & Object Example

In object-oriented programming, a class serves as a blueprint for creating objects. Just like a blueprint for a building specifies its structure, a class specifies the structure and behavior of objects created from it. For example, if a class represents a building with 100 windows, each object instantiated from that class would initially have 100 windows. However, the properties of objects can often be manipulated after instantiation, allowing for dynamic changes to their characteristics

Class components

  • Data(the attribute about it)
  • behavior(the mothods)

Method

  • A python method is like a python function
  • It mus be called on an object
  • It ust put it inside a class
  • A method has a name, and may take parameters and have any a raturn statement.

class and object example


class Car:
    def __init__(self, color, brand):
        self.color = color
        self.brand = brand

    def display(self):
        print(f"This car is a {self.color} {self.brand}")

# Create objects
mustang = Car("green", "Ford")
prius = Car("red", "Toyota")
golf = Car("blue", "Volkswagen")

# Call the display method
mustang.display()
prius.display()
golf.display()

Constructor

  • A special kind of method we use to initialize instance members of that class
  • It is used for initializing the instance members when we create the object of a class.
  • If you create four objects, the class constructor is called four times.
  • Every class must have a constructor, even if it simply relies on the default constructor.
  • Constructors can be of two types.
    • Non-parameterized Constructor (Default constructor)
    • Parameterized Constructor

Python __init__

  • __init__ is a reserved method in python classes.
  • It is known as a constructor in OOP concepts.
  • This method is called when an object is created from the class and it allows the class to initialize the attributes of a class.
  • It accepts the self-keyword as a first argument which allows accessing the attributes or method of the class.
  • We can pass any number of arguments at the time of creating
  • the class object, depending upon the __init__() definition.

Non-parameterized Constructor(default constructor)

  • When we do not include the constructor in the class or forget to declare it, then that becomes the default constructor.
  • It does not perform any task but initializes the objects
  • In the following example, we do not have a constructor but still we are able to create an object for the class.


class Employee:
        pass 
emp1 = Employee()
emp2 = Employee()
    

class Employee:
        def __init__(self):
            print("Employee object created")
emp1 = Employee()
emp2 = Employee()
    

Output


        Employee object created
        Employee object created
    

Python Parameterized Constructor

  • The Parameterized constructor has multiple parameters along with the self
  • It accepts the arguments during object creation

class Employee:
        def __init__(self, name):
            self.name = name 
            print(self.name, "created")
emp1 = Employee("John")
emp2 = Employee("Bob")
    

Output


John created 
Bob created
    

Instance method

  • Instance method are methods which require an object of its class to be created before it can be called.
  • Instance methods need a class instance and can access the instance through self.
  • Instance method takes more that one parameter, self, which points to an instance of class when the method is called
  • The self parameter, instance methods can freely access attributes and other methods on the same object


    class Employee:
            # Parameterized constructor
            def __init__(self, name, id):
                self.id = id # instance variable
                self.name = name # instance variable
                print(self.name, "created")

            # instance method
            def display(self):
                print(self.name, self.id)

    emp1 = Employee("John", 333)
    emp2 = Employee("Bob", 063)
        

Output


    John 333
    Bob 063