Learn-Object_Oriented_programming-with-us

Learn By Topic:

          

Class-objects

          

Inheritance

          

Polymorphism

          

Encapsulation

          

Abstraction

          

          

Click here to toggle b/w normal and focus mode: NORMAL MODE

What is an Object?

Object is an instance of a class. An object in OOPS is nothing but a self-contained component which consists of methods and properties to make a particular type of data useful. For example color name, table, bag, barking. When you send a message to an object, you are asking the object to invoke or execute one of its methods as defined in the class.From a programming point of view, an object in OOPS can include a data structure, a variable, or a function. It has a memory location allocated. Java Objects are designed as class hierarchies.

What is a class?

Declaring and intializing class and objects

Class

Like function definitions begin with the def keyword in Python, class definitions begin with a class keyword.

class students:
    pass

A class creates a new local namespace where all its attributes are defined. Attributes may be data or functions.There are also special attributes in it that begins with double underscores.

Class and Instance Variables

Instance variables are for data, unique to each instance and class variables are for attributes and methods shared by all instances of the class. Instance variables are variables whose value is assigned inside a constructor or method with self whereas class variables are variables whose value is assigned in the class.

Object

class object could be used to access different attributes.It can also be used to create new object instances (instantiation) of that class. The procedure to create an object is similar to a function call.

student_1 = student()

This will create a new object instance named student_1_. We can access the attributes of objects using the object name prefix. Attributes may be data or method. Methods of an object are corresponding functions of that class.

Python constructors

This a brief intro to Class and objects in python, we'll extend our knowledge in the subject by looking at real world problem

Let's say you're HOD assigned you a task to create a list of students and their details in you're classroom, he asked to you to include name,roll num,gender, year,dept and blood grp. Lets solve this using concept of class and objects.

class students:
    # The init method or constructor
    def __init__(self,name,roll,gender,year,dept,bloodgrp):
        # Instance Variable
        self.name = name
        self.roll = roll
        self.gender = gender
        self.year = year
        self.dept = dept
        self.bloodgrp = bloodgrp

Here we created a class named 'students', we initialized the class with init() method, in the init() method we provided our required arguments or called attributes nam,roll etc. finally we intialized them with self method.

Let's recap what we done:

class - A user-defined blueprint for an object that combines a set of attributes that describes the properties of that object. We used the 'Dog' class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.

Data members - It is a class variable or instance variable that holds properties associated with a class and its objects. Here, the data members are color and name.

self - This is a default parameter in every method in a class, even if we have no arguments to pass. This parameter has no value. Python provides the value to self when the method is called. In this example, when we call the method func() from object obj1 as obj1.func(), this is automatically converted into Dog.func(obj1) by Python.

init - The init represents constructor in python. this is used to initialize the object’s state. a constructor also contains some scripts that are executed at the time of Object creation. It is called as soon as an object of a class is instantiated.

Method - It is a user-defined function that can be defined inside or outside a class definition. It also contains a set of statements to execute. Here, func() is an example of a method.

Now, the user can create as many objects as he wants and can access data members, methods, and class variables from it. A user can define one or more classes and perform different actions using objects. We will learn more in detail in further articles.

Lets create some objects

std1 = students("SasiVatsal","20L31A5413",'male','2nd','Ai&ds','O+')
std2 = students("Dattu Rebel","20L31A5446",'male','2nd','Ai&ds','AB+')
std3 = students("Datla Mowli","20L31A5419",'male','2nd','Ai&ds','O-')
std4 = students("Malla Harsha","20L31A5444",'male','2nd','Ai&ds','B+')
std5 = students("Pramod Kumar","21L31A5406",'male','2nd','Ai&ds','O+')
std6 = students("Ritesh","20L31A5430",'male','2nd','Ai&ds','B-')
std7 = students("Monish","20L31A5440",'male','2nd','Ai&ds','B+')
std8 = students("Karthik","20L31A5446",'male','2nd','Ai&ds','O+')
std9 = students("Likhithr","21L31A5409",'male','2nd','Ai&ds','AB-')

Here std1 to std9 are the objects of the class students, likewise 'x' no.of objects can be created from a class and memory is allocated explicitly for every object.

lets create a method in the class to print student information

def print_info(self):
        print("name is :",self.name)
        print("roll no is :",self.roll)
        print("gender is :",self.gender)
        print("year is :",self.year)
        print("dept is :",self.dept)
        print("Blood gourp is: ",self.bloodgrp)

Now by tailoring everything together looks like this:

# creating class
class students:
    # The init method or constructor
    def __init__(self,name,roll,gender,year,dept,bloodgrp):
        # Instance Variable
        self.name = name
        self.roll = roll
        self.gender = gender
        self.year = year
        self.dept = dept
        self.bloodgrp = bloodgrp
    # a method inside class
    def print_info(self):
        print("name is :",self.name)
        print("roll no is :",self.roll)
        print("gender is :",self.gender)
        print("year is :",self.year)
        print("dept is :",self.dept)
        print("Blood gourp is: ",self.bloodgrp)
# creating objects
std1 = students("SasiVatsal","20L31A5413",'male','2nd','Ai&ds','O+')
std2 = students("Dattu Rebel","20L31A5446",'male','2nd','Ai&ds','AB+')
std3 = students("Datla Mowli","20L31A5419",'male','2nd','Ai&ds','O-')
std4 = students("Malla Harsha","20L31A5444",'male','2nd','Ai&ds','B+')
std5 = students("Pramod Kumar","21L31A5406",'male','2nd','Ai&ds','O+')
std6 = students("Ritesh","20L31A5430",'male','2nd','Ai&ds','B-')
std7 = students("Monish","20L31A5440",'male','2nd','Ai&ds','B+')
std8 = students("Karthik","20L31A5446",'male','2nd','Ai&ds','O+')
std9 = students("Likhithr","21L31A5409",'male','2nd','Ai&ds','AB-')

# iterating through objects
for student in (std1,std2,std3,std4,std5,std6,std7,std8,std9):
    student.print_info()
    print("\n")

output :

name is : SasiVatsal
roll no is : 20L31A5413
gender is : male
year is : 2nd
dept is : Ai&ds
Blood gourp is:  O+

name is : Dattu Rebel
roll no is : 20L31A5446
gender is : male
year is : 2nd
dept is : Ai&ds
Blood gourp is:  AB+

name is : Datla Mowli
roll no is : 20L31A5419
gender is : male
year is : 2nd
dept is : Ai&ds
Blood gourp is:  O-

name is : Malla Harsha
roll no is : 20L31A5444
gender is : male
year is : 2nd
dept is : Ai&ds
Blood gourp is:  B+

name is : Pramod Kumar
roll no is : 21L31A5406
gender is : male
year is : 2nd
dept is : Ai&ds
Blood gourp is:  O+

name is : Ritesh
roll no is : 20L31A5430
gender is : male
year is : 2nd
dept is : Ai&ds
Blood gourp is:  B-

name is : Monish
roll no is : 20L31A5440
gender is : male
year is : 2nd
dept is : Ai&ds
Blood gourp is:  B+

name is : Karthik
roll no is : 20L31A5446
gender is : male
year is : 2nd
dept is : Ai&ds
Blood gourp is:  O+

name is : Likhithr
roll no is : 21L31A5409
gender is : male
year is : 2nd
dept is : Ai&ds
Blood gourp is:  AB-

I hope you're clear with concept of objects and class, try practicing with you're own examples for better understanding, doing is what matters. Even if you feel you did'nt understand don't worry we will be dealing with class and objects in later sections of this tutorial in inheritance, polymorphism etc, until then..

Thank you. Peace ✌️.


Made with ❤️ by SasiVatsal and Mowli