What is Inheritance in Java?
Inheritance is one of the core concepts of Object-Oriented Programming (OOP) in Java. It allows a class (child or subclass) to inherit properties and behaviors (fields and methods) from another class (parent or superclass).
Syntax:
class Parent {
// fields and methods
}
class Child extends Parent {
// additional fields and methods
}
extends: keyword used to inherit a class
Why Use Inheritance?
- Code Reusability β Write once, reuse in multiple places
- Organized Code β Common logic in base class, specific logic in child class
- Polymorphism Support β One interface, multiple behaviors
Types of Inheritance in Java:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- (Java does not support multiple inheritance with classes directly but supports it through interfaces)
Example 1: Single Inheritance
// Parent class
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Child class
class Dog extends Animal {
void bark() {
System.out.println("Dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // inherited method
d.bark(); // child method
}
}
Output:
This animal eats food.
Dog barks.
Example 2: Multilevel Inheritance
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Weeping...");
}
}
public class Main {
public static void main(String[] args) {
Puppy p = new Puppy();
p.eat();
p.bark();
p.weep();
}
}
Output:
Eating...
Barking...
Weeping...
Example 3: Hierarchical Inheritance
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Cat c = new Cat();
Dog d = new Dog();
c.sound();
c.meow();
d.sound();
d.bark();
}
}
Output:
Animal makes sound
Cat meows
Animal makes sound
Dog barks
Important Keyword: super
The super keyword is used to:
- Refer to the parent class constructor
- Access parent class methods or fields
Example:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
super.sound(); // calls parent method
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
Output:
Animal sound
Dog barks
Java Doesnβt Support Multiple Inheritance (with classes)
// This will cause an error in Java
class A { }
class B { }
class C extends A, B { } // Not allowed
π Use interfaces instead for multiple inheritance.
Summary Table:
| Term | Meaning |
|---|---|
extends |
Keyword for inheritance |
super |
Refers to parent class members |
override |
Subclass can override parent methods |
Object class |
Superclass of all Java classes |
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
Β© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
