Ad Code

Responsive Advertisement

Basic Python Program - 51

Question 51: Write a Python program to create a class 'Degree' having a method 'getDegree' that prints "I got a degree". It has two subclasses namely 'Undergraduate' and 'Postgraduate' each having a method with the same name that prints "I am an Undergraduate" and "I am a Postgraduate" respectively. Call the method by creating an object of each of the three classes.
Download hole Program / Project code, by clicking following link:
Write a program in Python to demonstrate following operations:
    (a) Method overloading         (b) Method overriding

Python program that demonstrates both method overloading and method overriding:
# Base class
class Animal:
     def speak(self):
         print("Animal speaks")
     def move(self):
         print("Animal moves")
# Derived class
   class Dog(Animal):
        def speak(self):
             print("Dog barks")
         def move(self):
             print("Dog runs")
# Method Overloading
class Calculator:
        def add(self, a, b):
             return a + b
        def add(self, a, b, c):
              return a + b + c
# Main program
if __name__ == "__main__":
    # Method Overriding
     animal = Animal()
     dog = Dog()
     print("Method Overriding:")
     animal.speak() # Calls Animal's speak method
     animal.move() # Calls Animal's move method
     dog.speak() # Calls Dog's speak method (overrides Animal's speak)
     dog.move() # Calls Dog's move method (overrides Animal's move)
     # Method Overloading
     calculator = Calculator()
     result1 = calculator.add(1, 2)
     result2 = calculator.add(1, 2, 3)
     print("\nMethod Overloading:")
     print("Result 1:", result1) # Calls the add method with two arguments
     print("Result 2:", result2) # Calls the add method with three arguments

In this program:
  1. The Animal class is the base class with speak and move methods. The Dog class is a derived class that inherits from Animal. It overrides the speak and move methods to provide its own implementations.
  2. Method Overriding is demonstrated by creating instances of Animal and Dog classes and calling their methods. The derived class (Dog) overrides the methods from the base class (Animal), providing its own implementations.
  3. The Calculator class demonstrates Method Overloading by having two methods with the same name add but different numbers of parameters. In Python, only the latest defined method with the same name is considered, so method overloading as seen in other languages is not supported natively. However, you can achieve it by using default values for some arguments or by using variable-length argument lists (e.g., *args or **kwargs).
Note: Method overloading, as it exists in some other languages, is not directly supported in Python because Python allows only one method with a given name in a class. To achieve similar functionality, you can use default arguments or variable-length argument lists, but that's a different concept than traditional method overloading. If you have specific questions about method overloading in Python, please feel free to ask.
Programming Code:
Following code write in: BP_P51.py
# Python Program

# Super class & Subclass
# Super Class

class Degree():

    # Super Class Method
    def getDegree(self):
        print("I got a degree.")

# Subclass  1
class Undergraduate(Degree):

    # Subclass Method
    def show(self):
        print("I am an Undergraduate.")

# Subclass  2
class Postgraduate(Degree):

    def show(self):
        print("I am a Postgraduate.")

# Creating object

deg = Degree()
under_deg = Undergraduate()
post_deg = Postgraduate()

# Call Methods
deg.getDegree()
under_deg.show()
post_deg.show()

# Call Superclass Method
print("---------------------------------")
under_deg.getDegree()
post_deg.getDegree()

# Thanks for Reading.
Output:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement