Sobes.tech
Junior — Middle

What happens if a method existing in the base class is overridden in a derived class?

sobes.tech AI

Answer from AI

If a method in a child class overrides a method that exists in the base class, then when this method is called through an object of the child class, the version of the method from the child class will be executed.

This is called polymorphism and allows modifying or extending the behavior of the base class.

Example:

class Base:
    def greet(self):
        print("Hello from Base")

class Child(Base):
    def greet(self):
        print("Hello from Child")

obj = Child()
obj.greet()  # Will output: Hello from Child

If the method is not overridden, the method from the base class will be called.