0% found this document useful (0 votes)
7 views9 pages

pwp notes 5

The document covers Object Oriented Programming in Python, detailing the creation of classes and objects, method overloading and overriding, data hiding, and various types of inheritance. It provides definitions, examples, and advantages of key concepts such as encapsulation and code reusability. Additionally, it illustrates different inheritance types including single, multiple, hierarchical, multilevel, and hybrid inheritance with code snippets.

Uploaded by

milee1722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

pwp notes 5

The document covers Object Oriented Programming in Python, detailing the creation of classes and objects, method overloading and overriding, data hiding, and various types of inheritance. It provides definitions, examples, and advantages of key concepts such as encapsulation and code reusability. Additionally, it illustrates different inheritance types including single, multiple, hierarchical, multilevel, and hybrid inheritance with code snippets.

Uploaded by

milee1722
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit-V Object Oriented Programming in Python

5.1 Creating Classes and Objects.


Q.1.Define class and object in python. - 2 MKS
Q.2.Write a program to create class EMPLOYEE with ID and NAME and display its
contents. - 4 MKS
Q.3.Write a program to create class student with Roll no. and Name and display its
contents. - 6 MKS

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)

5.2Method Overloading and Overriding


Q.1.Explain method overloading and overriding in python. - 4 MKS
Q.2. Explain method overloading in python with example. - 6 MKS

Method Overloading (Same method name, different parameters):

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.

Method Overriding in Python

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.

5.3 Data Hiding in Python


Q.1.Define Data Hiding concept ? Write two advantages ofData Hiding.
Data hiding is an essential concept of object-oriented programming (OOP) that helps in
restricting access to certain details of an object. It is used to prevent direct modification of an
object's internal data from outside the class, ensuring that sensitive information is well-protected
and only accessible through controlled mechanisms.

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.

Advantages of Data Hiding

✅ Encapsulation: Protects data from unintended modifications.​


✅ Security: Prevents unauthorized access to sensitive information.​
✅ Data Integrity: Ensures that data remains valid and consistent.​
✅ Code Maintainability: Changes in internal implementation do not affect external code.​
✅ Better Control: Allows data access through getter and setter methods.
5.4 Inheritance in Python
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class
(called a child or derived class) to inherit attributes and methods from another class (called a
parent or base class). This promotes code reuse, modularity, and a hierarchical class structure.

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

Key Benefits of Inheritance:


Code Reusability: By inheriting from a parent class, the child class can reuse code, reducing
redundancy.
Extensibility: The child class can extend or modify the behavior of the parent class.
Polymorphism: Inheritance supports polymorphism, allowing methods to behave differently
based on the object that invokes them.
Types of Inheritance in Python:
SINGLE INHERITANCE
Definition: Single inheritance occurs when a child class derives from only one parent class. This
is the most straightforward form of inheritance.

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)

# Creating object of class C


obj = C()
obj.Add() # Inherited from class A
obj.Sub() # Inherited from class A
obj.Multi() # Inherited from class B
obj.Div() # Inherited from class B
obj.Rem() # Defined in class C

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 B(A): # Class B inherits from class A


def Multi(self):
print("Multiplication: ", self.num1 * self.num2)
def Div(self):
print("Division: ", self.num1 / self.num2)

class C(A): # Class C also inherits from class A (Hierarchical inheritance)


def Rem(self):
print("Remainder: ", self.num1 % self.num2)

# Creating objects for both derived classes


obj_b = B() # Object of class B
obj_b.Add() # Inherited from class A
obj_b.Sub() # Inherited from class A
obj_b.Multi()
obj_b.Div()

obj_c = C() # Object of class C


obj_c.Add() # Inherited from class A
obj_c.Sub() # Inherited from class A
obj_c.Rem()

Multi Level Inheritance


Definition: Multilevel inheritance involves a chain of classes where a class derives from another
derived class. This creates a hierarchy of inheritance.

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)

class C(B): # Multilevel inheritance (class C inherits from class B)


def Rem(self):
print("Remainder: ", self.num1 % self.num2)

# Creating object of class C


obj = C()
obj.Add() # Inherited from class A
obj.Sub() # Inherited from class A
obj.Multi() # Inherited from class B
obj.Div() # Inherited from class B
obj.Rem() # Defined in class C

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()

You might also like