Professional Documents
Culture Documents
Cse Assignment 2
Cse Assignment 2
Q2. Write the differences between instance method, class method and static
method.
Ans. The instance methods are bound to the class instance and perform a set of
actions on the data/value given by the object (instance) variables. If we use the
instance variable inside the methods, these methods are called instance methods. It
can modify the object state. The self keyword is the first parameter to work with
the instance method, and the self refers to the current object.
The class methods are bound to the class, not to the instance. It can modify the
class state means it can change class configuration globally. It can access only the
class variable. The class methods are used to create the factory methods. The
syntax of class methods is different; instead of taking self parameter, they accept
cls as a parameter that points to the class. It can't modify the instance state.
However, the changes made by the class method reflect all instances of the class.
The @classemethod decorator or classmethod() defines the class methods.
The Static methods neither use self nor cls parameter; general utility methods
perform the task in isolation. Static methods in Python are similar to those found in
Java and C++, and they can't modify the behavior of the class or instance. The
@staticmethod decorator or staticmethod() method defines the static methods.
Q4. What is inheritance? Explain the different types of inheritance with example.
Ans. Inheritance is the process of creating a new Class, called the Derived Class ,
from the existing class, called the Base Class . The Inheritance has many
advantages, the most important of them being the reusability of code. Rather than
developing new Objects from scratch, new code can be based on the work of other
developers, adding only the new features that are needed. The reuse of existing
classes saves time and effort.
Different types of inheritance are –
Single Inheritance
Multi Level Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Multiple Inheritance
Single inheritance-
In this inheritance, a derived class is created from a single base class.
class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print(" this is function 2 ")
ob = Child()
ob.func1()
ob.func2(
Multiple inheritance-
In this inheritance, a derived class is created from more than one base class.
class Parent:
def func1(self):
print("this is function 1")
class Parent2:
def func2(self):
print("this is function 2")
class Child(Parent , Parent2):
def func3(self):
print("this is function 3")
ob = Child()
ob.func1()
ob.func2()
ob.func3()
Hybrid inheritance-
This is a combination of more than one inheritance. Hence, it may be a
combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel
inheritance Hierarchical and Multipath inheritance, or Hierarchical, Multilevel and
Multiple inheritances.
class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child1(Parent):
def func3(self):
print(" this is function 3"):
ob = Child3()
ob.func1()
Hierarchical Inheritance-
In this inheritance, more than one derived class is created from a single base class
and further child classes act as parent classes for more than one child class.
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Parent):
def func3(self):
print("this is function 3")
ob = Child()
ob1 = Child2()
ob.func1()
ob.func2()
class Shape:
def __init__(self, name):
self.name = name
def area(self):
pass
def fact(self):
return "I am a two-dimensional shape."
def __str__(self):
return self.name
class Square(Shape):
def __init__(self, length):
super().__init__("Square")
self.length = length
def area(self):
return self.length**2
def fact(self):
return "Squares have each angle equal to 90 degrees."
class Circle(Shape):
def __init__(self, radius):
super().__init__("Circle")
self.radius = radius
def area(self):
return pi*self.radius**2
a = Square(4)
b = Circle(7)
print(b)
print(b.fact())
print(a.fact())
print(b.area())
def __str__(self):
return "({0},{1})".format(self.x, self.y)
p1 = Point(1, 2)
p2 = Point(2, 3)
print(p1+p2)
Q7. Write a program that takes an input from the user and asks to enter filename. It
then opens the file for reading it. If the file is not found, catch the exception and
give a suitable message.
Ans.
filenm=input(“enter the file name”)
try:
fp=open(filenm,”r”)
print(fp.read())
except FileNotFoundError:
print(f “{filenm} does not exist”)