How can I create a polymorphism of classes?

Hi, can I make a class with a lot of classes in the inside of this one? Its the only way I think one can accomplished polymorphism. Also, If I call a superclass that has a lot of subclasses, will it “call” only the superclass or will it call the superclass, I mean by this if I will only be able to use modify my data with the method inside the superclass, or if I call my superclass will I be able to use the methods inside that superclass?

That would not be the way to go.

based on the explanation in this lesson:

https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-classes/lessons/inheritance-and-polymorphism/exercises/polymorphism

The following example qualifies as polymorphism:

class A:
    def abc(self):
        print('hello a')

class B:
    def abc(self):
        print('hello b')

class C:
    def abc(self):
        print('hello c')

a = A() 
a.abc()
b = B() 
b.abc()
c = C() 
c.abc()

the same method does different things on different classes. Same for the + (see example in the lesson).

this is known as MRO, here is a an article:

https://makina-corpus.com/blog/metier/2014/python-tutorial-understanding-python-mro-class-search-path

that is a bit too much for a single forum post.

I have made an example, that is more practical:

class Circle:
    def __init__(self, radius):
        self.pi = 3.1415
        self.radius = radius

    def get_area(self):
        return self.radius ** 2 * self.pi

class Square:
    def __init__(self, length_a):
        self.length_a = length_a

    def get_area(self):
        return self.length_a ** 2

class Rectangle(Square):
    def __init__(self, length_a, length_b):
        super().__init__(length_a)
        self.length_b = length_b
    
    def get_area(self):
        return self.length_a * self.length_b
1 Like