You are on page 1of 4

Chapter (8)

Class and Object


Object

Python is an object-oriented programming language. So, its main focus is on objects unlike
procedure oriented programming languages which mainly focuses on functions.In object oriented
programming language, object is simply a collection of data (variables) and methods (functions) that
act on those data.

Class

A class is a blueprint for the object. Let's understand it by an example: (Suppose a class is a
prototype of a student. A student contains all the details about the name, age etc. So student is a
class and can create many objects from a class.) An object is also called an instance of a class and
the process of creating this object is known as instantiation.

Create the “StudentClass.py”

class Student:

name=""

age=0

def SetData(self):

self.name=input("Enter Student Name : ")

self.age=input("Enter Age : ")

def Display(self):

print ("\nStudent Name = ",self.name)

print ("Age = ",self.age)

s=Student()

s.SetData()

s.Dislay()

Constructor

A constructor is a special type of method (function) which is used to initialize the instance members
of the class. Constructor can be parameterized and non-parameterized as well. Constructor
definition executes when create object of the class. Constructors also verify that there are enough
resources for the object to perform any start-up task.A constructor is a class function that begins
with double underscore (_). The name of the constructor is always the same __init__().

Create the “StudentConstructor.py”

Python Programming Ch8-1


KMD Computer Centre
class Student:

name=""

age=0

def __init__(self):

self.name=input("Enter Student Name : ")

self.age=input("Enter Age : ")

def Display(self):

print ("\nStudent Name = ",self.name)

print ("Age = ",self.age)

s=Student()

s.Display()

Inheritance

Inheritance is a feature of Object Oriented Programming. It is used to specify that one class will get
most or all of its features from its parent class. It is a very powerful feature which facilitates users to
create a new class with a few or more modification to an existing class. The new class is called child
class or derived class and the main class from which it inherits the properties is called base class or
parent class. The child class or derived class inherits the features from the parent class, adding new
features to it. It facilitates re-usability of code.

Create the “Inheritance.py”

class People(object):

Name=""

def __init__(self):

self.Name=input("Enter Name : ")

def Display(self):

print ("\nName = " , self.Name)

class Student(People):

Grade=""

Python Programming Ch8-2


KMD Computer Centre
def __init__(self):

super(Student,self).__init__()

self.Grade=input("Enter Grade : ")

def Display(self):

super(Student,self).Display()

print ("Grade = " , self.Grade)

class Employee(People):

Salary=0

def __init__(self):

super(Employee,self).__init__()

self.Salary=input("Enter Salary : ")

def Display(self):

super(Employee,self).Display()

print ("Salary = " , self.Salary)

print ("\nFor Student ...\n")

s=Student()

s.Display()

print ("\nFor Employee ...\n")

e=Employee()

e.Display()

Python Programming Ch8-3


KMD Computer Centre
Create the “MultiInheritance.py”

class People(object):

Name=""

def __init__(self):

self.Name=raw_input("Enter Name : ")

def Display(self):

print "\nName = " , self.Name

class UnderGraduate(object):

Major=""

def __init__(self):

super(UnderGraduate,self).__init__()

self.Major=raw_input("Enter Major : ")

def Display(self):

super(UnderGraduate,self).Display()

print "Major = " , self.Major

class Graduate(UnderGraduate,People):

Degree=""

def __init__(self):

super(Graduate,self).__init__()

self.Degree=raw_input("Enter Degree : ")

def Display(self):

super(Graduate,self).Display()

print "Degree = " , self.Degree

print "\nFor Graduate ...\n"

g=Graduate()

g.Display()

Python Programming Ch8-4

You might also like