0
votes

What is the difference between the two?

A super class having explore() and an inheriting class having the same method, Is this overriding or polymorphism?

I am clear with the difference b/w overriding and overloading, but the polymorphism and overriding seems the same. Or are they?

class A:
    def explore(self):
        print("explore() method from class A")
class B(A):
    def explore(self):
        super().explore() # calling the parent class explore() method
        print("explore() method from class B") 
b_obj = B()
b_obj.explore()
2

2 Answers

1
votes

Polymorphism is a wide concept which includes overriding and overloading and much more. It is just the ability of an object to provide multiple behaviors.

Overriding and overloading are two examples on how you can achieve polymorphism.

The following related question might be useful:

Are polymorphism, overloading and overriding similar concepts?

1
votes

Overriding - An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

Where in Polymorphism can be achieved through overriding.

A Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class. Polymorphism is where you are not sure of the objects type at runtime and the most specific method is called. Therefore the behaviour of the method called may differ, depending on the objects type at runtime.