Inheritance is a central concept in object-oriented programming (OOP) and is often regarded as one of the building blocks for increasing efficiency. In this tutorial, you will deepen your understanding of inheritance in Java. You will learn how to work with access modifiers, use constructors, and the role encapsulation plays in inheritance.
Key insights
- Inheritance allows the inheriting of properties and methods of a class.
- The access modifier private restricts access to variables or methods, while protected allows extended access within classes that inherit from the original class.
- Constructors must be called in the subclass to instantiate objects correctly.
- Interface methods can be generalized when having multiple specific classes.
Step-by-step guide
Access modifiers: protected vs. private
If you declare a variable (e.g., color) as protected in a class like Vehicle, you can use this variable directly in a derived class (e.g., Car). However, if you declare the variable as private, you cannot directly access it in the subclass.

This specifically means that access to private variables is only possible through setters and getters.
The example shows that we must use setFarbe and getFarbe to access the private variable color.

If the variable is declared as protected, you can simply access it within the Car class and change the value.
Using constructors in inheritance
When defining a subclass, you must ensure that the constructor of the superclass is called correctly.

This ensures that the color is initialized correctly when creating a Car object.

Make sure to define a constructor in the subclass that appropriately passes the parameters of the superclass.
Inheriting and calling methods
Two classes, Vehicle and Car, allow you to use common variables and methods. When you define a method in Vehicle, you can also use that method in Car without rewriting it.

Here, the method vehicleColorOutput is called and outputs the color value provided by the getColor method.
Access restrictions on methods
If you try to call a method in a subclass that is not available for the class, you will get a compilation error. In our example, a Vehicle object must be passed to call the getColor method.

This practically means that you must ensure that the method matches your data type before calling it. Therefore, you can pass a bike, a car, or both to a method that simply expects Vehicle objects.
Example of calling instance methods in static contexts
Note that in static methods, the this keyword cannot be used as it does not refer to a specific instance. In such cases, you might want to use another method or restructure the logic.

This call outputs the color for the myCar object correctly.
Summary - Inheritance in Java: A Beginner's Guide
Inheritance is an important concept in programming with Java that helps you modularize and reuse code. In this tutorial, you have learned the basics of access modifiers, constructors, and method calls in relation to inheritance.
Frequently Asked Questions
How can I access private variables in the subclass?You cannot access private variables directly in the subclass. Use getter and setter methods.
What is the difference between protected and private?protected allows access in the subclass and the same package, while private is only accessible within its own class.
Do I need to define constructors in the subclass?Yes, if you have modified the constructor of the superclass, you must also define the constructors in the subclass accordingly.
Can I freely override methods from superclasses in subclasses?Yes, you can override methods that are not marked with final or static in the subclass to implement specific behavior.