Skip to main content

3 docs tagged with "inheritance"

View all tags

Composition vs. Inheritance

In Object-Oriented Programming, there are two primary ways to build relationships between classes and reuse code: Inheritance and Composition. While we have already covered inheritance, it's crucial to understand its alternative, composition, and to know when to choose one over the other. This choice is one of the most fundamental design decisions you will make when structuring your programs.

Inheritance: Creating Derived Classes and Using `super()`

We have mastered the art of creating self-contained classes that bundle data and behavior. But what if we have multiple classes that share some common logic? For example, a Dog, a Cat, and a Fish are all types of Animal. They might all have a name and an age, but each makes a different sound.

Polymorphism: Method Overriding and Duck Typing

We've learned about inheritance and how a child class can inherit attributes and methods from a parent class. Now we're going to explore polymorphism, a powerful concept that allows us to treat objects of different classes in a similar way.