Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Inheritance Basics | Inheritance and Polymorphism
Python Object-Oriented Programming (OOP) Mastery

Inheritance Basics

Stryg for at vise menuen

12345678910111213
class Animal: def __init__(self, name): self.name = name def speak(self): return f"{self.name} makes a sound" class Dog(Animal): pass dog = Dog("Buddy") print(dog.name) # Output: Buddy print(dog.speak()) # Output: Buddy makes a sound

Inheritance allows you to create a new class (called a subclass) that is based on an existing class (called a base class or superclass). The subclass automatically inherits all the attributes and methods of the base class, so you do not have to rewrite code that is common to both classes. In the example above, the Animal class defines a constructor and a speak method. The Dog subclass inherits both from Animal. When you create an instance of Dog, you can access the name attribute and the speak method as if they were defined in Dog itself. This makes your code more reusable and organized, especially when you want to represent different types of objects that share common behaviors.

1234567891011
class Dog(Animal): def fetch(self, item): return f"{self.name} fetches the {item}" def speak(self): base_sound = super().speak() return f"{base_sound}, but {self.name} barks!" dog = Dog("Buddy") print(dog.fetch("ball")) # Output: Buddy fetches the ball print(dog.speak()) # Output: Buddy makes a sound, but Buddy barks!

You can extend the functionality of a subclass by adding new methods or by overriding existing ones. In the revised Dog class, a new method called fetch is added, which is specific to dogs. The speak method is also overridden to provide a more specific implementation. To include behavior from the base class, you can use the super() function, which allows you to call methods from the superclass. This is useful when you want to enhance, not completely replace, the original method. Python determines which method to call using the method resolution order (MRO), which follows the inheritance chain from the subclass up to its ancestors.

question mark

Which statement about inheritance in Python is correct?

A) A subclass cannot access methods of its base class.

B) The super() function lets a subclass call methods from its base class.

C) Only one subclass can inherit from a base class.

D) Inheritance forces subclasses to override all base class methods.

Which of the following is true about the Dog class in the examples above?

A) It inherits the speak method from Animal and cannot override it.

B) It can add new methods, like fetch, that are not present in Animal.

C) It cannot use the name attribute from Animal.

D) It must define its own __init__ method to work.

What does the super().speak() call do in the Dog class's speak method?

A) Calls the Dog class's own speak method.

B) Calls the speak method of the Animal class.

C) Causes a runtime error.

D) Calls all methods in the inheritance chain.

Vælg alle korrekte svar

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Sektion 3. Kapitel 1
some-alt