#7 Known is a drop! Inheritance in JAVA

Published: (March 3, 2026 at 10:25 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

Inheritance in Java

Inheritance is a powerful object‑oriented programming feature offered by Java.
It allows you to create a new class by reusing the functionality of an existing class.
The existing (more general) class is the parent class, and the new (more specialized) class is the child class.

“Is‑A” Relationship

  • The extends keyword is used for class inheritance.
  • Every class implicitly extends java.lang.Object, making Object the ultimate superclass.
  • This is a classic example of an is‑a relationship: a String is a Object, a Car is a Vehicle, etc.

Note: A class always extends another class (directly or indirectly).

Types of Inheritance

TypeDescription
Single InheritanceOne subclass inherits from one superclass. This is the simplest form and establishes a straightforward “is‑a” relationship.
Multilevel InheritanceA chain of inheritance: a class inherits from a base class, and that derived class becomes a base class for another class.
Hierarchical InheritanceMultiple subclasses inherit from a single superclass.
Multiple InheritanceNot supported through classes in Java.

Why Java Does Not Support Multiple Inheritance (Classes)

Consider three classes: A, B, and C.
Class C tries to extend both A and B, each of which defines a method msg() with different implementations.

  • This creates the Diamond Problem: when C calls msg(), the compiler cannot determine which implementation to use.
  • Java resolves this ambiguity by disallowing multiple class inheritance, resulting in a compile‑time error.
  • The same error occurs even if the methods have different signatures.

Work‑around: Use interfaces to achieve multiple inheritance of type.

Important Rules of Java Inheritance

  • You cannot assign a superclass object to a subclass reference.
  • A class declared final cannot be extended.
  • A class cannot extend itself.
  • A class can extend only one class.
  • Assigning a subclass object to a superclass reference is called upcasting.
  • Constructors, static initialization blocks (SIB), and instance initialization blocks (IIB) of the superclass are not inherited, but they are executed when creating a subclass object.
  • Static methods of a superclass are accessible from a subclass but are hidden, not overridden.
0 views
Back to Blog

Related posts

Read more »