← Back to Core Java Course | Chapter 8: Inheritance | Lesson 2 of 7

Java Single Inheritance

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?

java
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);
	}
}

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

java
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
	}
}

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

java
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();
	}
}

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

java
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();
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.