-->

Classes In Python

Question
CBSEENCO12011541

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.

Solution
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 ”.

Some More Questions From Classes in Python Chapter

Differentiate between static and dynamic binding in Python? Give the suitable example of each.

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.

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.

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

Differentiate between static and dynamic binding in Python? Give suitable examples of each.

What is the significance of super() method? Give an example of the same.

How do we implement abstract method in python? Give an example for the same.