pwp notes 5
pwp notes 5
Q.4.Design a class student with data members : name, roll no., department, mobile no.
Create suitable methods for reading and printing student information. 6 MKS
In Python, you define a class by using the class keyword followed by a name and a
colon. Then you use .__init__() to declare which attributes each instance of the class
should have:
class Employee:
def __init__(self, name, age):
self.name = name
self.age = age
Creating object
Object: An instance of a class that holds the actual data and behaviors defined in the
class
Syntax:
obj = MyClass()
print(obj.x)
Method overloading allows a class to have multiple methods with the same name but
different parameters. However, Python does not support traditional method overloading
as seen in languages like Java. Instead, Python handles similar functionality using default
arguments or by checking arguments within a single method.
Definition: Method overriding happens when a subclass provides its own version of a method
that is already defined in its superclass. The method in the subclass has the same name and
parameters (signature) as the method in the superclass, but it gives a new implementation.
By restricting direct access to data, data hiding promotes encapsulation, which enhances code
security, prevents unintended modifications, and ensures that object properties remain in a
consistent and valid state.
1.Parent Class:
This is the base class from which other classes inherit.
It contains attributes and methods that the child class can reuse.
2.Child Class:
This is the derived class that inherits from the parent class.
The syntax for inheritance is class ChildClass(ParentClass).
The child class automatically gets all attributes and methods of the parent class unless overridden
class A:
num1=int(input("Enter First Number: "))
num2=int(input("Enter Second Number: "))
def Add(self):
print("Addition: ", self.num1+self.num2)
def Sub(self):
print("Subtraction: ", self.num1-self.num2)
class B(A):
def Multi(self):
print("Multiplication: ", self.num1*self.num2)
def Div(self):
print("Division: ", self.num1/self.num2)
def Rem(self):
print("Remainder: ", self.num1%self.num2)
obj=B()
obj.Add()
obj.Sub()
obj.Multi()
obj.Div()
obj.Rem()
Multiple inheritance
Definition: Multiple inheritance allows a child class to inherit from more than one parent class,
enabling the combination of functionalities from multiple sources.
class A:
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
def Add(self):
print("Addition: ", self.num1 + self.num2)
def Sub(self):
print("Subtraction: ", self.num1 - self.num2)
class B:
def Multi(self):
print("Multiplication: ", obj.num1 * obj.num2)
def Div(self):
print("Division: ", obj.num1 / obj.num2)
class C(A, B): # Multiple inheritance (class C inherits from both class A and class B)
def Rem(self):
print("Remainder: ", self.num1 % self.num2)
Hierarchical inheritance
Definition: Hierarchical inheritance occurs when multiple child classes inherit from a single
parent class. This allows for shared behavior among different classes.
class A:
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
def Add(self):
print("Addition: ", self.num1 + self.num2)
def Sub(self):
print("Subtraction: ", self.num1 - self.num2)
class A:
num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))
def Add(self):
print("Addition: ", self.num1 + self.num2)
def Sub(self):
print("Subtraction: ", self.num1 - self.num2)
class B(A):
def Multi(self):
print("Multiplication: ", self.num1 * self.num2)
def Div(self):
print("Division: ", self.num1 / self.num2)
Hybrid inheritance
Definition: Hybrid inheritance combines two or more types of inheritance, resulting in a more
complex relationship between classes.
class A:
def method1(self):
print('A class method1')
class B(A):
def method2(self):
print('B class method2')
class C(A):
def method3(self):
print('C class method3')
class D(B,C):
def method4(self):
print('D class method4')
d=D()
d.method1()
d.method2()
d.method3()