[Link]: Write a program to create Classes and Objects in Python.
Program:
class Person:
def __init__(self, name, age):
[Link] = name # This is an instance variable
[Link] = age
person1 = Person("krishna", 25)
person2 = Person("radha", 30)
print([Link])
print([Link])
output:
krishna
30
[Link]: Write a program to implement inheritance concept.
Program:
class Animal:
# attribute and method of the parent class
name = ""
def eat(self):
print("I can eat")
# inherit from Animal
class Dog(Animal):
# new method in subclass
def display(self):
# access name attribute of superclass using self
print("My name is ", [Link])
# create an object of the subclass
Dg = Dog()
# access superclass attribute and method
[Link] = input("enter the name:")
[Link]()
# call subclass method
[Link]()
Output:
enter the name: snoopy
I can eat
My name is snoopy
3. Aim:Write a Python program to implement method overloading, constructor overloading
program for method overloading:
class MathOperations:
def add(self, a, b=None, c=None):
if b is not None and c is not None:
return a + b + c
elif b is not None:
return a + b
else:
return a
# Example Usage
math_obj = MathOperations()
result1 = math_obj.add(5)
result2 = math_obj.add(5, 10)
result3 = math_obj.add(5, 10, 15)
# Output
print(result1)
print(result2)
print(result3)
Output:
5
15
30
Program for Constructor overloading:
class Person:
def __init__(self, name, age=None):
[Link] = name
[Link] = age
# Example Usage
person1 = Person("shiny")
person2 = Person("srinith", 8)
# Output
print([Link], [Link])
print([Link], [Link])
output:
shiny None
srinith 8