Inheritance
Definition: Inheritance in java is a mechanism in which one object or a child class acquires all the properties and behaviours of parent object/class.
Explanation: Imagine you have a family tree. At the very top, you have your grandparents. They have children, who are your parents. Your parents, in turn, have you and your siblings. Each generation inherits some characteristics and traits from the previous generation.
For example, your grandparents might have passed down traits like hair color, eye color, and certain family traditions to your parents. Your parents, in turn, pass down these traits and traditions to you and your siblings. These properties are inherited from your grandparents which are like base/parent class, your parents are like sub-class / child class, and you are secondary sub-class of the primary sub-class .
Note: A base class is also known as parent class or super class & a child class is also known as derived class or sub class.
Extends Keyword
Extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.
Syntax:
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
Types of Inheritance:
-
Single
-
Multilevel
-
Hierarchical
Note: To access parent class you must create and use the object of child class. Using object of parent class causes error.
1.Single Inheritance
Single inheritance refers to a scenario where a class extends only one superclass. In other words, there is a single parent-child relationship between two classes. The child class inherits the properties and methods of its parent class.
Example:
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
}
class TestInheritance {
public static void main(String args[]) {
Dog d = new Dog();
d.bark();
d.eat();
}
}
2. Multi-level inheritance
Multi-level inheritance refers to a scenario where a derived class extends another derived class. This creates a parent-child-grandchild relationship between three classes. The child class inherits properties and methods from its immediate parent class, and in turn, the parent class also inherits from its parent class, and so on.
Example:
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
}
class BabyDog extends Dog {
void weep() {
System.out.println("weeping...");
}
}
class TestInheritance2 {
public static void main(String args[]) {
BabyDog d = new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
3.Hierarchial Inheritance
Hierarchical inheritance refers to a scenario where multiple classes inherit from a single superclass. In other words, there is a single parent class and multiple child classes.
Example:
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
}
class Cat extends Animal {
void meow() {
System.out.println("meowing...");
}
}
class TestInheritance3 {
public static void main(String args[]) {
Cat c = new Cat();
c.meow();
c.eat();
//c.bark();//Error
}
}
There’s another type of inheritance called Multiple inheritance. Java does not support multiple inheritance, but this can be achieved using interfaces, which we will discuss in the next article.