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 |
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.