Java Single Inheritance
In this page:
What is Single Inheritance?
Single inheritance means a class extends exactly one direct parent — Java deliberately restricts class inheritance to a single parent (unlike some other languages) specifically to avoid the ambiguity problems multiple class inheritance can create.
Example: What is Single Inheritance?
class Animal {}
class Dog extends Animal { // exactly one direct parent
}
public class Main {
public static void main(String[] args) {
System.out.println(new Dog() instanceof Animal);
}
}
Login to try C/C++/Java/PHP code in the editor
Code Reusability
A subclass automatically gains every public and protected member its parent defines, without retyping any of that code, which is the main code-reuse benefit inheritance provides over copy-pasting shared logic into every related class.
Example: Code Reusability
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
}
public class Main {
public static void main(String[] args) {
new Dog().eat(); // reused without retyping
}
}
Login to try C/C++/Java/PHP code in the editor
Adding New Methods
Beyond what it inherits, a subclass is free to declare entirely new fields and methods of its own that the parent class knows nothing about — inheritance only flows downward from parent to child, never the other way.
Example: Adding New Methods
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Bark"); // new method, parent knows nothing about it
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
}
}
Login to try C/C++/Java/PHP code in the editor
Private Members
Private members of the parent are deliberately invisible to the subclass, even though the subclass technically "has" them in memory — this preserves the parent's encapsulation, forcing the subclass to go through the parent's public or protected methods to interact with that hidden state.
Example: Private Members
class Animal {
private String secret = "hidden";
}
class Dog extends Animal {
void reveal() {
// System.out.println(secret); // would not compile: private to Animal
System.out.println("Cannot access private parent field");
}
}
public class Main {
public static void main(String[] args) {
new Dog().reveal();
}
}
Login to try C/C++/Java/PHP code in the editor
Reference Variables
Storing a subclass object in a variable typed as the parent class (Animal a = new Dog();) is a common and useful pattern that enables polymorphism — code written against the general Animal type can work correctly with any specific subclass passed in.
Example: Reference Variables
class Animal {
void sound() {
System.out.println("Some sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog(); // subclass stored in parent-typed variable
a.sound();
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: