1. Inheritance in Java
Inheritance is an OOP concept that allows one class to acquire the properties and methods of another class.
It helps us reuse existing code instead of writing the same code again.
Example from real life:
Consider a parent and a child. A child may inherit certain characteristics from their parents.
Similarly, in Java, a child class can inherit accessible fields and methods from a parent class.
Important Terminology
- Parent Class: The class whose properties and methods are inherited. It is also called the superclass.
- Child Class: The class that inherits from another class. It is also called the subclass.
- extends: The keyword used to inherit a class in Java.
2. Example of Inheritance
Let's create a parent class called Animal and a child class called Dog.
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
public class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.eat();
d1.bark();
}
}
Output
Animal is eating
Dog is barking
Explanation
-
Animalis the parent class. -
Dogis the child class. -
extendsis used to inherit theAnimalclass. -
eat()is inherited from the parent class. -
bark()belongs to the child class.
Here, the Dog object can access both eat() and bark() methods.
3. Types of Inheritance in Java
Java supports different types of class inheritance.
1. Single Inheritance
Single inheritance means one child class inherits from one parent class.
<pre> Animal | Dog </pre>
Example:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
Here, Dog inherits from Animal.
2. Multilevel Inheritance
Multilevel inheritance means a class inherits from another child class, forming a chain.
<pre> Animal | Dog | Puppy </pre>
Example:
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("Puppy is weeping");
}
}
public class Main {
public static void main(String[] args) {
Puppy p1 = new Puppy();
p1.eat();
p1.bark();
p1.weep();
}
}
Output:
Eating...
Barking...
Puppy is weeping
Here, Puppy inherits methods from both Dog and Animal.
3. Hierarchical Inheritance
Hierarchical inheritance means multiple child classes inherit from the same parent class.
<pre> Animal / \ Dog Cat </pre>
Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
}
public class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.eat();
d1.bark();
Cat c1 = new Cat();
c1.eat();
c1.meow();
}
}
Output:
Animal is eating
Dog is barking
Animal is eating
Cat is meowing
Here, both Dog and Cat inherit from the Animal class.
4. Multiple Inheritance
Multiple inheritance means one child class inherits from more than one parent class.
Java does not support multiple inheritance through classes because it can create ambiguity when parent classes contain methods with the same signature.
However, Java supports multiple inheritance of type through interfaces.
Example:
interface Father {
void showFather();
}
interface Mother {
void showMother();
}
class Child implements Father, Mother {
public void showFather() {
System.out.println("Father's properties");
}
public void showMother() {
System.out.println("Mother's properties");
}
}
public class Main {
public static void main(String[] args) {
Child c1 = new Child();
c1.showFather();
c1.showMother();
}
}
Output:
Father's properties
Mother's properties
Here, the Child class implements two interfaces: Father and Mother.
4. Important Keywords Used in Inheritance
Java provides some important keywords related to inheritance.
1. extends
The extends keyword is used when one class inherits another class.
class Dog extends Animal {
}
2. super
The super keyword is used to refer to the immediate parent class.
It can be used to:
- Access parent class variables.
- Call parent class methods.
- Call parent class constructors using
super().
Example:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void eat() {
super.eat();
System.out.println("Dog is eating");
}
}
Output when eat() is called:
Animal is eating
Dog is eating
Here, super.eat() calls the parent class method.
5. Advantages of Inheritance
Inheritance provides several benefits:
- Code Reusability: Reuse existing code from parent classes.
- Less Code Duplication: Avoid writing the same methods repeatedly.
- Easy Maintenance: Changes to inherited functionality can be made in the parent class.
- Extensibility: Add new features in child classes.
- Supports Polymorphism: Inheritance allows method overriding and runtime polymorphism.
Top comments (0)