Experiment 5-PY
Experiment 5-PY
THEORY:
Syntax-
class ClassName:
# class definition
Example-
class Bike:
name = ""
gear = 0
Syntax-
objectName = ClassName()
Example-
bike1 = Bike()
1. Python program to create and list of employees using Employee class. Program should
also print total number of employees.
Employee class should have:
● Empcount (class variable)
● Id and name (instance variables)
● constructor to set id
● set_name(), get_name() & get_id() methods (instance methods)
● set_emp_count() (class method)
CODE:
class Employee:
#class variable
empcount=0
@classmethod
def set_empcount(cls):
cls.empcount+=1
def setname(self,name):
self.name=name
def getname(self):
return self.name
def getid(self):
return self.eid
for i in L:
print("\n Employee Id:",i.getid(),"\t Employee Name:",i.getname())
OUTPUT:
THEORY:
INHERITANCE: One of the core concepts in object-oriented programming (OOP)
languages is inheritance. It is a mechanism that allows you to create a hierarchy of
classes that share a set of properties and methods by deriving a class from another class.
● Single inheritance: When a child class inherits from only one parent class, it is
called single inheritance.
● Multiple inheritances: When a child class inherits from multiple parent classes, it is
called multiple inheritances.
Syntax-
Class BaseClass:
{Body}
Class DerivedClass(BaseClass):
{Body}
MRO: In python, method resolution order (MRO) defines the order in which the base
classes are searched when executing a method. First, the method or attribute is searched
within a class and then it follows the order we specified while inheriting. This order is also
called Linearization of a class and set of rules are called MRO(Method Resolution Order).
While inheriting from another class, the interpreter needs a way to resolve the methods
that are being called via an instance. Thus we need the method resolution order. Using
super() accesses the next class in the MRO.
Three principles of MRO:
1. Search for the subclass before going for its baseclass.
2. If a class is inherited from several classes, it searches in the order from left to right.
3. It will not visit any class more than once
Example:
class A:
def __init__(self):
print("A")
super().__init__()
class B:
def __init__(self):
print("B")
super().__init__()
class C(object):
def __init__(self):
print("C")
super().__init__()
class X(A,B):
def __init__(self):
print("X")
super().__init__()
class Y(B,C):
def __init__(self):
print("Y")
super().__init__()
class P(X,Y,C):
def __init__(self):
print("P")
super().__init__()
pobj=P()
Therefore, MRO: P, X, A, Y, B, C.
CODE:
class Employee:
def setname(self,name):
self.name=name
def getname(self):
return self.name
def getid(self):
return self.eid
class Student:
def __init__(self,college):
self.college=college
def getcollege(self):
return self.college
def setdetails(self,name):
self.name=name
def getdetails(self):
print("\n E_id:",super().getid(),"\t Name:",super().getname(),"\t
College:",super().getcollege(),"\t Period:",self.period)
#main prog
intern=Intern(eid,college,period)
intern.setdetails(name)
intern.getdetails()
OUTPUT:
THEORY:
(ii) Method Overloading: By default, Python does not support method overloading, but
we can achieve it by modifying out methods.
Given a single function sum (), the number of parameters can be specified by you. This
process of calling the same method in different ways is called method overloading.
(iv) Method Overriding: By using method overriding a class may “copy” another class,
avoiding duplicated code, and at the same time enhance or customize a part of it. Method
overriding is thus a part of the inheritance mechanism.
In Python, method overriding occurs by simply defining the child class in a method with
the same name as a method in the parent class.
3. Python program to overload greater than (>) operator to make it act on user-defined
class objects.
CODE:
class Age:
def __init__(self, years):
self.years = years
output :
THEORY:
ABSTRACTION: A class that contains one or more abstract methods is called an abstract
class. An abstract method is a method that has a declaration but does not have an
implementation. While we are designing large functional units we use an abstract class.
When we want to provide a common interface for different implementations of a
component, we use an abstract class.
INTERFACES: An interface acts as a template for designing classes. Interfaces also
define methods the same as classes, but abstract methods, whereas class contains
nonabstract methods. Abstract methods are those methods without implementation or
which are without the body. So the interface just defines the abstract method without
implementation. The implementation of these abstract methods is defined by classes that
implement an interface.
Syntax-
An ABC is a simple interface or base class defined as an abstract class in nature, and the
abstract class has certain abstract methods. Following that, if any classes or objects
implement or drive from these base classes, these derived classes are required to
implement all of those methods.
import abc
class Boat(abc.ABC):
@abc.abstractmethod
def swim(self):
pass
CODE:
@abstractmethod
def disconnect(self):
pass
class IBM(Printer):
def printit(self,text):
print(text, " to IBM. ")
def disconnect(self):
print("Disconnected from IBM")
class HP(Printer):
def printit(self,text):
print(text, " to HP. ")
def disconnect(self):
print("Disconnected from HP")
#main prog
text="Connescted"
OUTPUT: