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: FOCUS 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?
Class are a blueprint or a set of instructions to build a specific type of object. It is a basic concept of Object-Oriented Programming which revolve around the real-life entities. Class in Java determines how an object will behave and what the object will contain.
Constructors are used for initializing new objects. Fields are variables that provides the state of the class and its objects, and methods are used to implement the behavior of the class and its objects. There are various types of classes that are used in real time applications such as nested classes, anonymous classes, lambda expressions.
I know you're little bit confued, let me get straight to the point, A class is a way of organizing information about a type of data so a programmer can reuse elements when making multiple instances of that data type—for example, if a programmer wanted to make three instances of Car, maybe a BMW, a Ferrari, and a Ford instance. The Car class would allow the programmer to store similar information that is unique to each car (they are different models, and maybe different colors, etc.) and associate the appropriate information with each car.
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
Class functions that begin with double underscore __ are called special functions as they have special meaning.
Of one particular interest is the init() function. This special function gets called whenever a new object of that class is instantiated.The init method is similar to constructors in C++ and Java. Constructors are used to initializing the object’s state. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation. It runs as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.
Another important function is The self , Class methods must have an extra first parameter in the method definition. We do not give a value for this parameter when we call the method, Python provides it. If we have a method that takes no arguments, then we still have to have one argument.The first argument of the function in class must be the object itself, This is conventionally called self. It can be named otherwise but we highly recommend to follow the convention.
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-