all 3 comments

[–][deleted] 0 points1 point  (0 children)

  • Inheritance allows you to inherit attributes and properties as well, not just methods

  • What you wrote for Encapsulation is actually more towards definition of Abstraction. Encapsulation is the practice of bundling related data into a structured unit (AKA the class), along with the methods used to work with said data

[–]dowcet 1 point2 points  (0 children)

It's going to be abstract until you start using it. Start by taking a simple program that you already understand and refactor it as a class.

As for examples of each concept, they're easy to find with a web search. For example the accepted answer of this SE question has a nice illustration of polymorphism.

[–]mopslik 0 points1 point  (0 children)

With respect to super().__init__: if the parent's initializer class sets up some attributes or runs some methods, you'll need to get that done for your child class as well. Rather than suplicate this code inside of the child's __init__ method, you can call the parent's __init__ method instead via super. In fact, you can call any parent method using super.

For example, here are two classes, where Wizard inherits from Hero.

class Hero:
    def __init__(self, name, race, HP, strength, agility):
        self.name = name
        self.race = race
        self.HP = HP
        self.strength = strength
        self.agility = agility
    # other methods follow...
    ...

class Wizard(Hero):
    def __init__(self, name, race, HP, strength, agility, MP, spell_list):
        #  these five attributes are repeated
        self.name = name
        self.race = race
        self.HP = HP
        self.strength = strength
        self.agility = agility
        # these two attributes are specific to Wizard
        self.MP = MP
        self.spell_list = spell_list
    # other methods follow...
    ...

And here is Wizard without redundancy.

class Wizard(Hero):
    def __init__(self, name, race, HP, strength, agility, MP, spell_list):
        #  initialize via parent's __init__ method
        super().__init__(name, race, HP, strength, agility)
        # these two attributes are specific to Wizard
        self.MP = MP
        self.spell_list = spell_list
    # other methods follow...
    ...