Inheritance
Inheritance's main idea is to use already defined classes and produce new ones by improving and extending some other classes.
Some class Animal
contains all common attributes and methods for all animals. The Cat
class inherits the Animal
class.
123456789class Animal: pass # Inheriting the Animal class class Cat(Animal): pass print(Animal) print(Cat)
Note
- The common class (that is inherited by other classes) is called a superclass or parent class;
- The class that inherits is called a child class;
- The child class has all the same methods and attributes that parents have and can have additional ones, but not vice versa;
- Each class in Python can have multiple parent classes and/or children's classes;
- Each class in Python is a children's class of
Object
class.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Still meg spørsmål om dette emnet
Oppsummer dette kapittelet
Vis eksempler fra virkeligheten
Awesome!
Completion rate improved to 7.69
Inheritance
Sveip for å vise menyen
Inheritance's main idea is to use already defined classes and produce new ones by improving and extending some other classes.
Some class Animal
contains all common attributes and methods for all animals. The Cat
class inherits the Animal
class.
123456789class Animal: pass # Inheriting the Animal class class Cat(Animal): pass print(Animal) print(Cat)
Note
- The common class (that is inherited by other classes) is called a superclass or parent class;
- The class that inherits is called a child class;
- The child class has all the same methods and attributes that parents have and can have additional ones, but not vice versa;
- Each class in Python can have multiple parent classes and/or children's classes;
- Each class in Python is a children's class of
Object
class.
Takk for tilbakemeldingene dine!