Introduction
Inheritance is one of the core concepts of Object-Oriented Programming (OOP) in Java. It allows one class to acquire the properties and behaviors (methods and variables) of another class. This helps in code reusability, easier maintenance, and establishing a hierarchical relationship between classes.
In simple words, inheritance means deriving a new class from an existing one.
What is Inheritance in Java?
Inheritance in Java is a mechanism where a child class (subclass) inherits features from a parent class (superclass).
It allows you to reuse existing code and add new features without modifying the original class.
To implement inheritance in Java, we use the extends keyword.
Syntax of Inheritance
class Parent {
// parent class members
}
class Child extends Parent {
// child class members
}
Here,
-
Parent→ Base or Superclass -
Child→ Derived or Subclass
The subclass automatically gets access to all non-private members of the superclass.
Example of Inheritance in Java
// Parent class
class Animal {
void eat() {
System.out.println("Animals can eat");
}
}
// Child class
class Dog extends Animal {
void bark() {
System.out.println("Dogs can bark");
}
}
// Main class
public class InheritanceExample {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // inherited method
d.bark(); // own method
}
}
Output:
Animals can eat
Dogs can bark
Explanation:
The Dog class inherits the eat() method from the Animal class using the extends keyword. Thus, an object of the Dog class can call both its own method (bark()) and the inherited method (eat()).
Types of Inheritance in Java
Java supports several types of inheritance:
-
Single Inheritance
→ One class inherits from another.
Example:B extends A -
Multilevel Inheritance
→ A class inherits from another class, which itself inherits from another class.
Example:C extends B extends A -
Hierarchical Inheritance
→ Multiple classes inherit from the same parent class.
Example:B extends A,C extends A -
Hybrid Inheritance (Combination of above types)
→ Not directly supported in Java because it can lead to ambiguity. -
Multiple Inheritance
→ Not supported with classes in Java (to avoid ambiguity). However, it can be achieved using interfaces.
Example of 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 MultilevelInheritance {
public static void main(String[] args) {
Puppy p = new Puppy();
p.eat();
p.bark();
p.weep();
}
}
Output:
Eating...
Barking...
Weeping...
Advantages of Inheritance
-
Code Reusability: You can reuse methods and variables from the parent class.
-
Improved Maintenance: Changes made in the parent class automatically reflect in child classes.
-
Extensibility: You can easily extend existing functionality.
-
Readability: Establishes a clear class hierarchy and relationship.
Important Points to Remember
-
Java supports single, multilevel, and hierarchical inheritance through classes.
-
Multiple inheritance is only possible using interfaces.
-
The
superkeyword is used to access parent class members and constructors. -
Private members of a superclass are not accessible in subclasses directly.
-
Constructors are not inherited, but the subclass constructor can call the superclass constructor using
super().
Conclusion
Inheritance is a powerful feature in Java that encourages code reuse, modularity, and scalability. By understanding how inheritance works, you can design applications that are easier to extend and maintain. Whether it’s simple single inheritance or complex interface-based multiple inheritance, mastering this concept is crucial for every Java developer.
Comments
Post a Comment