Sponsor Area
Define a class RING in Python with following specifications
Instance Attributes
- RingID # Numeric value with a default value 101
- Radius # Numeric value with a default value 10
- Area # Numeric value
Methods:
- AreaCal() # Method to calculate Area as
# 3.14*Radius*Radius
- NewRing() # Method to allow user to enter values of
# RingID and Radius. It should also
# Call AreaCal Method
- ViewRing() # Method to display all the Attributes 4
class RING: # OR class RING( ): OR class RING(Object):
def __init__(self):
self.RingID=101
self.Radius=10
self.Area=0
def AreaCal(self):
self.Area=3.14*self.Radius*self.Radius
def NewRing(self):
self.RingID=input('Enter RingID')
self.Radius=input('Enter radius')
self.AreaCal() # OR AreaCal(self)
def ViewRing(self):
print self.RingID
print self.Radius
print self.Area
Differentiate between static and dynamic binding in Python? Give the suitable example of each.
|
Static Binding |
Dynamic Binding |
Time of binding |
happens during compilation |
binding happens at run time |
Actual object |
Actual object is not used for binding |
Actual object is used for binding |
Example |
Method overloading |
Method overriding |
Methods of binding |
Private, static and final methods show static binding. Because they cannot be overridden. |
Other than private, static and final methods show dynamic binding. Because they can be overridden. |
Class |
Type class |
Object class |
Write two methods in Python using the concept of Function Overloading (Polymorphism) to perform the following operations:
(i) A function having one argument as side, to calculate Area of Square as side*side
(ii) A function having two arguments as Length and Breadth, to calculate Area of Rectangle as Length*Breadth.
def Area(side):
print side*side
def Area(length,breadth):
print length*breadth
If you run the code, the second Area(B, H) definition will overwrite/override the first one because python does not support function overloading “ as illustrated in the example shown above ”.
class Exam:
Regno=1
Marks=75
def __init__(self,r,m): #function 1
self.Regno=r
self.Marks=m
def Assign(self,r,m): #function 2
Regno = r
Marks = m
def Check(self): #function 3
print self.Regno, self.Marks
print Regno, Marks
(i) In the above class definition, both the functions - function 1 as well as function 2 have similar definition. How are they different in execution?
(ii) Write statements to execute function 1 and function 2.
(i) Function 1 is the constructor which gets executed automatically as soon as the object of the class is created. Function 2 is a member function which has to be called to assign the values to Regno and Marks.
(ii) Function 1 E1=Exam(1,95) # Any values in the parameter
Function 2 E1.Assign(1,95) # Any values in the parameter
Define a class BOX in Python with following specifications
Instance Attributes
- BoxID # Numeric value with a default value 101
- Side # Numeric value with a default value 10
- Area # Numeric value with a default value 0
Methods:
- ExecArea() # Method to calculate Area as
# Side * Side
- NewBox() # Method to allow user to enter values of
# BoxID and Side. It should also
# Call ExecArea Method
- ViewBox() # Method to display all the Attributes
class BOX:
def __init__(self):
self.BoxID=101
self.Side=10
self.Area=0
def ExecArea(self):
self.Area=self.Side*self.Side
def NewBox(self):
self.BoxID=input('Enter BoxID')
self.Side=input('Enter side')
self.ExecArea()
def ViewBox(self):
print self.BoxID
print self.Side
print self.Area
Differentiate between static and dynamic binding in Python? Give suitable examples of each.
|
Static Binding |
Dynamic Binding |
Time of binding |
happens during compilation |
binding happens at run time |
Actual object |
Actual object is not used for binding |
Actual object is used for binding |
Example |
Method overloading |
Method overriding |
Methods of binding |
Private, static and final methods show static binding. Because they cannot be overridden. |
Other than private, static and final methods show dynamic binding. Because they can be overridden. |
Class |
Type class |
Object class |
What is the significance of super() method? Give an example of the same.
Super() function is used to call base class methods which have been extended in the derived class.
Example-
class GradStudent ( Student ):
def __init__ ( self ):
super ( GradStudent , self ). __init__ ()
self . subject = ''
self . working = ''
def readGrad ( self ):
# Call readStudent method of parent class
super ( GradStudent , self ). readStudent ()
How do we implement abstract method in python? Give an example for the same.
Abstract method: An unimplemented method is called an abstract method. When an abstract method is declared in a base class, the derived class has to either define the method or raise 'NotImplementedError'.
class Shape(object):
def findArea(self):
pass
class Square(Shape):
def __init__(self,side):
self.side = side
def findArea(self):
return self.side * self.side
Sponsor Area
Sponsor Area