Inheritance in Java
0 132
Introduction to Inheritance in Java
Inheritance in Java is one of the most powerful features of object-oriented programming. It allows a class to inherit fields and methods from another class, enabling code reuse, reducing redundancy, and establishing relationships between classes in a hierarchical structure.
What is Inheritance?
In Java, inheritance enables a class (known as a subclass or derived class) to acquire properties and behaviors from another class (known as a superclass or base class). The subclass can also override inherited methods to provide its own specific behavior.
Syntax of Inheritance
Inheritance is implemented using the extends
keyword.
class Parent {
void show() {
System.out.println("Parent method");
}
}
class Child extends Parent {
void display() {
System.out.println("Child method");
}
}
Types of Inheritance in Java
Java supports the following types of inheritance:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance (via Interfaces)
Single Inheritance
In single inheritance, a subclass inherits from one superclass. It is the most basic form of inheritance.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
Multilevel Inheritance
In multilevel inheritance, a class inherits from a class which itself inherits from another class. This forms a chain of inheritance.
class Animal {
void eat() {
System.out.println("Animal eats.");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("Mammal walks.");
}
}
class Human extends Mammal {
void speak() {
System.out.println("Human speaks.");
}
}
Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses inherit from a single superclass.
class Animal {
void breathe() {
System.out.println("Animals breathe.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows.");
}
}
Multiple Inheritance using Interfaces
Java does not support multiple inheritance with classes to avoid the "Diamond Problem". However, it can be achieved using interfaces.
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
class Duck implements Flyable, Swimmable {
public void fly() {
System.out.println("Duck flies.");
}
public void swim() {
System.out.println("Duck swims.");
}
}
The super
Keyword
The super
keyword is used to call parent class methods, constructors, or access parent class fields when they are hidden by child class members.
class Parent {
void display() {
System.out.println("Parent class");
}
}
class Child extends Parent {
void display() {
super.display();
System.out.println("Child class");
}
}
Advantages of Inheritance
- Promotes code reusability
- Reduces redundancy
- Supports method overriding for polymorphic behavior
- Helps in creating a logical class hierarchy
Conclusion
Inheritance in Java is essential for designing robust and scalable applications. By understanding different types of inheritance—single, multilevel, hierarchical, and multiple via interfaces—you can write cleaner, more organized, and reusable code. Mastery of inheritance will allow you to build better object-oriented designs and manage complexity more efficiently.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments