Inheritance is one of the concepts that determine whether you can structure more complex programs cleanly. Perhaps you have already written objects and functions and are now wondering how you can avoid reprogramming what different objects have in common over and over again. That is exactly what inheritance is for. In this tutorial, I guide you through the basics using a simple example with cars: from the first blueprint through inheritance via prototype to polymorphism, where one and the same method delivers different results depending on the object.
Key Takeaways
Inheritance means that a class takes over the properties and functions of another class. In JavaScript, this happens via the prototype. A subclass can adopt inherited methods, define its own values, or override methods. If a method is overridden, the affected object behaves differently, while other objects of the superclass remain unchanged. This principle is called polymorphism. The superclass is also called the parent class or base class, and the inheriting class the child class or subclass.
Step-by-Step Guide
Step 1: Create the blueprint for a car
Start with a constructor function named Auto. It serves as a blueprint and accepts two values: the name and the number of seats. In the constructor, you assign both values to the newly created object with this.name and this.sitze. Additionally, you can specify that every car has four tires, because this basic property applies to all cars, regardless of their design.

Step 2: Create an object from the blueprint
With the keyword new, you create a concrete object from the constructor function. In the video, this object is called simples Auto. You pass in the name VW Golf and the seat count of five. With that, you have a simple car object that has all the properties of the blueprint.
Step 3: Add a method for driving
Now create a function fahren on the prototype of the class Auto. It outputs the name of the car together with the note that the vehicle is traveling fully occupied. You assume here that all seats are occupied, so the object's seat count is sufficient. Then call the method on the object. Note that the output initially does not appear in the browser as long as you do not print it.
Step 4: Make the output visible in the browser
So that the result appears on the page, you use document.write. You pass it the return value of the method fahren. After saving and refreshing the page, the text VW Golf fährt mit fünf Leuten appears. Pay attention to clean syntax: a wrongly placed character immediately leads to an error message in the console.
Step 5: Let a race car inherit from Auto
Now the actual inheritance comes into play. Create a new constructor function Rennwagen and assign a new object of the class Auto to its prototype. In this way, the race car inherits all properties and methods of the parent class, in particular the function fahren. This corresponds to the idea that parents pass on their properties to their children.
Step 6: Define your own values for the subclass
However, the race car has a fixed number of seats, namely exactly one seat. Therefore, in the constructor of the race car, you only take the name from the passed arguments and set the seats permanently to the value 1. Although the race car descends from the car, it deliberately deviates at this point, much like a child who differs from their parents in certain behaviors.
Step 7: Create and output a race car object
Create an object named Formel 1 from the class Rennwagen and pass it the name Ferrari. You do not have to specify the seat count, since it is already defined in the constructor. Output the method fahren of the object with document.write. The result now reads Ferrari fährt mit einem Sitz. The object uses the inherited method but uses the own values of the subclass.
Step 8: Override an inherited method
Now I'll show you how to deliberately adapt an inherited method. Create your own function fahren on the prototype of the class Rennwagen. This new version does not return the name, but its own text, for example Rennwagen fährt mit nur einem Sitz. The method of the parent class remains untouched. The override affects only the Rennwagen class and all objects created from it.
Step 9: Check the effects on other objects
Now compare the results. The simple car object still outputs VW Golf drives with five people, even though the method was changed in the subclass. The race car, on the other hand, delivers its new output. That is exactly polymorphism: The same method call fahren leads to different behavior depending on the object. The override therefore only affects the subclass and leaves the superclass untouched.
Step 10: Classifying the technical terms of inheritance
With inheritance, you will encounter several terms that describe the same thing. The inheriting class, in this example Auto, is called a superclass, parent class, or base class. In English, one speaks of a parent. The inheriting class, that is Rennwagen, is referred to as a subclass, derived class, or child class, and in English as a child. When you read these terms in documentation or tutorials, you immediately know which class is meant.
Step 11: Recognizing the practical benefit of inheritance
Imagine you are developing a database class with basic functions such as saving, reading, and overwriting. You want to build a specialized database for artists without changing the base class, because other developers might use it as well. Through inheritance, you merely add the specific data, such as artist name and age. The benefit also shows in games: All enemies share a basic behavior but differ in color and damage. Inheritance thus saves you repetition and keeps larger projects well organized.

Step 12: Solving your own challenge
Now try to create a class Alltagswagen yourself that inherits from Auto and always has five seats. Create an object of it and output the method fahren. Start with a constructor that takes the name and sets the seats fixed to five. Then link the prototype to an Auto object. This gives you a class that takes over all properties of the superclass but specifies a fixed value.
Step 13: Overriding the method in the Alltagswagen
Add your own function fahren to the prototype of the class Alltagswagen that returns a text, for example Alltagswagen drives. Which text exactly you enter is secondary to understanding. The only important thing is that you override the method. Then create an object called Mein Auto with the name A3 and call the method. The output is again done via document.write.
Step 14: Finding errors and checking the result
If an error message appears, the browser console will help you. In the video, for example, a missing semicolon in a specific line is pointed out, which arose from an accidentally placed colon. Correct the error, reload the page, and check the three outputs: VW Golf drives with five people, Rennwagen drives with only one seat, and Alltagswagen drives one and free. With that, you have practically applied inheritance and polymorphism.
Summary – Inheritance in JavaScript: Understanding prototypes and polymorphism
You have seen how to create a blueprint with a constructor function, generate objects from it, and let one class inherit from another via the prototype. Custom values in the subclass enable specialized objects, while overridden methods show how polymorphism works. The superclass remains unchanged, and that is precisely the big advantage of this concept. If you maintain commonalities centrally in a superclass, you save time, reduce errors, and keep an overview even as projects grow.
FAQ
What is the difference between inheritance and polymorphism?Inheritance describes that a class takes over properties and methods of another class. Polymorphism means that the same method can show different behavior depending on the object, for example when a subclass overrides the method.
Why is the prototype used in the example?In JavaScript, inheritance happens via the prototype. An object automatically looks for missing properties and methods in its prototype, which gives the subclass access to the functions of the superclass.
Can an overridden method change the parent class?No. The override only affects the subclass and the objects created from it. Objects of the superclass keep their original behavior.
When is the use of inheritance worthwhile?Inheritance is worthwhile especially in larger or complex programs, for example when several developers work in a team or similar objects with a common basic structure are needed.
