Java Inheritance
- What is Inheritance in Java?
- The extends Keyword in Java
- Superclass Reference to Subclass Object
- Multilevel Inheritance in Java
- Constructor Execution Order
- Common Mistakes
- FAQ – Inheritance in Java
- Conclusion
Inheritance in Java is one of the core concepts of Object-Oriented Programming (OOP). It allows a class to reuse fields and methods of another class using the extends keyword. In this guide, you will learn how inheritance works, see real examples, and understand how to use it in practice.
1. What is Inheritance in Java?
Inheritance is a mechanism where one class (called a subclass) inherits properties and behavior from another class (called a superclass).
This relationship is often described as an IS-A relationship:
- Dog IS-A Animal
- Car IS-A Vehicle
Inheritance helps to:
- Reuse existing code
- Reduce duplication
- Improve maintainability
- Support polymorphism
Read in more details on Inheritance (object-oriented programming).
2. The extends Keyword in Java
To create inheritance in Java, you use the extends keyword:
class SubClass extends SuperClass {
// class body
} Example:
public class Box6 {
double width;
double height;
double depth;
Box6(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
public Box6() {}
double getVolume() {
return width * height * depth;
}
} Now we create subclasses:
public class ColorBox extends Box6 {
String color;
public ColorBox(int width, int height, int depth, String color) {
this.width = width;
this.height = height;
this.depth = depth;
this.color = color;
}
} public class HeavyBox extends Box6 {
int weight;
public HeavyBox(int width, int height, int depth, int weight) {
this.width = width;
this.height = height;
this.depth = depth;
this.weight = weight;
}
} Here, both ColorBox and HeavyBox inherit fields and methods from Box6.
3. Superclass Reference to Subclass Object
In Java, a superclass reference can point to a subclass object:
Box6 box = new HeavyBox(10, 10, 10, 5); This is called upcasting and is widely used in polymorphism.
However, the reverse is not allowed:
// Compilation error
// HeavyBox box = new Box6(); Important rule:
The accessible members are determined by the reference type, not the object type.
4. Multilevel Inheritance in Java
Java supports multilevel inheritance:
class Shipment extends HeavyBox {
double cost;
Shipment(int w, int h, int d, int weight, double cost) {
super(w, h, d, weight);
this.cost = cost;
}
} Hierarchy example:
- Box6 → HeavyBox → Shipment
The subclass inherits everything from all levels.
5. Constructor Execution Order
Constructors are executed from superclass to subclass:
class E {
E() { System.out.println("E"); }
}
class F extends E {
F() { System.out.println("F"); }
}
class G extends F {
G() { System.out.println("G"); }
} Output:
E
F
G 6. Common Mistakes
- Trying to access private fields directly
- Confusing reference type with object type
- Forgetting to call
super() - Incorrect use of inheritance instead of composition
7. FAQ – Inheritance in Java
What is inheritance in Java?
Inheritance allows a class to reuse fields and methods from another class.
What is the extends keyword?
The extends keyword is used to create a subclass from a superclass.
Does Java support multiple inheritance?
Java does not support multiple inheritance with classes, but it supports it with interfaces.
Why use inheritance?
To reduce code duplication and improve structure and scalability.
8. Conclusion
Understanding Java inheritance is essential for mastering OOP. It helps you write cleaner, reusable, and scalable code. Practice using the extends keyword and build class hierarchies to fully understand this concept.
Please log in or register to have a possibility to add comment.