Loading...
  • Bansal Computer Center
  • punjabedu.bansalcenter.com
Code Crafter IT Innovation
#
Code Crafter IT Innovation
Loading...
  • Bansal Computer Center
  • punjabedu.bansalcenter.com

Python Method Overriding

.

.


Method Overriding - Definition




Method Overriding - Key Points




Method Overriding - Basic Example



class Parent:
    def show(self):
        print("This is the Parent class method")

class Child(Parent):
    def show(self):
        print("This is the Child class method (Overridden)")

c = Child()
c.show()  # Output: This is the Child class method (Overridden)
      


Method Overriding - Using super()



class Parent:
    def show(self):
        print("Parent class method")

class Child(Parent):
    def show(self):
        print("Child class method starts...")
        super().show()  # Calls parent method
        print("Child class method ends...")

c = Child()
c.show()
# Output:
# Child class method starts...
# Parent class method
# Child class method ends...
      


Method Overriding - Different Logic



class BankAccount:
    def interest_rate(self):
        return 5  # Default interest rate

class SavingsAccount(BankAccount):
    def interest_rate(self):
        return 7  # Higher interest rate

sa = SavingsAccount()
print(sa.interest_rate())  # Output: 7
      


Method Overriding - Multiple Inheritance


class A:
    def display(self):
        print("Class A")

class B(A):
    def display(self):
        print("Class B")

class C(A):
    def display(self):
        print("Class C")

class D(B, C):
    pass

obj = D()
obj.display()       # Output: Class B (MRO: D -> B -> C -> A)
print(D.mro())      # Shows Method Resolution Order
      

.

What students Say?

Here are the stdudents...

#

Contact Us

Empower your tech dreams with us. Get in touch!

  • #
  • #
  • #
  • #
  • #