Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Inheritance

Наследование

Inheritance lets you build one class on top of another. The new class can reuse behavior from the existing class, add methods of its own, and replace selected methods with its own versions.

What inheritance means

A subclass is a class defined in terms of another class, called its base class or superclass. Think of a notification that can describe how it will be delivered. An email notification can use the same code to build that description as other notifications, while supplying its own delivery channel. You only need to write the part that differs.

What happens when you call a method?

A subclass can leave an inherited method alone, introduce a new method, or override an existing method where the language permits it. Overriding means replacing the implementation of an operation for that subclass. It isn't the same as overloading, where methods share a name but accept different parameter lists.

Here is a small Java example:

class Notification {
    String channel() { return "generic"; }
    String label() { return "Channel: " + channel(); }
}

class EmailNotification extends Notification {
    @Override
    String channel() { return "email"; }
}

In the same package, suppose you write Notification n = new EmailNotification();. The call n.label() returns "Channel: email". The subclass doesn't redefine label(), so the base implementation runs. When that implementation calls channel() on the same object, Java selects the email version. This is dynamic dispatch: which implementation runs depends on the actual object, not just the declared type of the variable. It lets code work with notifications through one common type without checking each object's exact class.

What a subclass does not get

The details vary by language. In Java, a class can extend only one class, although it can implement multiple interfaces. A subclass doesn't inherit constructors: creating a subclass instance still invokes a superclass constructor to initialize the superclass's part of the object. And although the object has the superclass's private state, subclass code can't access those private fields directly. It can use methods the superclass exposes instead.

When to use inheritance

Start with the callers: could they use a subclass instance anywhere they expect the base type without changing their code or breaking their assumptions? If a base operation promises to accept an empty list, an override shouldn't start rejecting it. Keeping these promises is the idea behind the Liskov substitution principle.

If you only want to borrow functionality, consider composition. A notification service can hold a delivery transport and ask it to send messages; the service itself isn't a transport. Passing in a different transport changes delivery without adding another level to a class hierarchy. That also makes it easier to test the service with a substitute transport.

In interviews

Expect to trace a short example like the one above, or to defend a design choice. For a method call in Java, explain two separate steps: the compiler determines which signature the call refers to, and for an overridden instance method the runtime selects the implementation for the actual object.