Inheritance
Inheritance simply means re-usability, inheritance allows a class to use the data members(properties) and member function(methods) of another class.
Inheritance is one of the best features of Object Oriented Programming.
Inheritance represents the IS-A relationship between the Superclass and its Subclass, it also known as the parent-child relationship.
In above syntax extends is a keyword, which is used to extend a class in a class declaration. Basically extends keyword is used to increase the properties and behaviors of child(Sub class) class by using Parent(Super class) class.
class Employee
{
int salary = 20000;
}
class Developer extends Employee
{
int age = 26;
public static void main(String args[])
{
Developer d = new Developer();
System.out.println("salary of developer "+d.salary);
System.out.println("age of developer "+d.age);
}
}
output: salary of developer 20000
age of developer 26
Here, Employee, is super class and Developer is a subclass. The relationship between two classes is Developer IS-A Employee i.e Developer is a type of employee class.
In the above diagram, Multiple and Hybrid inheritance are not supported in java through the class but it is possible through the interface in java.
Inheritance is one of the best features of Object Oriented Programming.
Inheritance represents the IS-A relationship between the Superclass and its Subclass, it also known as the parent-child relationship.
Real life example of inheritance
- suppose there is parent and child relationship, child reuse(inherit) the surname of his father and other qualities.
For example:
Ram Singh → Manav Singh
(parent) inherit (child)
Advantages of Inheritance
- For code re-usability.
- To achieve method overriding(dynamic binding) or runtime polymorphism.
- Application development time is less.
- Application taking a less memory.
Some important points
- The class which gives data members and methods is known as super or parent or base class in inheritance.
- The class which is taken the data members and methods is known as sub or child or derived a class in inheritance.
Syntax of Inheritance:
class Subclass-Name extends Superclass-Name
{
//data members or fields
//member functions or methods
}
In above syntax extends is a keyword, which is used to extend a class in a class declaration. Basically extends keyword is used to increase the properties and behaviors of child(Sub class) class by using Parent(Super class) class.
Simple Example of Inheritance
class Employee
{
int salary = 20000;
}
class Developer extends Employee
{
int age = 26;
public static void main(String args[])
{
Developer d = new Developer();
System.out.println("salary of developer "+d.salary);
System.out.println("age of developer "+d.age);
}
}
output: salary of developer 20000
age of developer 26
Here, Employee, is super class and Developer is a subclass. The relationship between two classes is Developer IS-A Employee i.e Developer is a type of employee class.
Types of inheritance
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
In the above picture, Java supports only three types of inheritance through class. These are single, multilevel, hierarchical inheritance. Multiple and Hybrid inheritance are not supported in java through the class but these are supported through interface concept.
1) Single Inheritance
Single inheritance is the simple inheritance, when a class extends another class(Only one class) then we call it as single inheritance. In single inheritance there exists single super class(parent class) and single sub class(child class).
For example:
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking");
}
public static void main(String args[])
{
Dog d = new Dog();
d.eat();
d.bark();
}
}
output: eating
barking
👉 : By default, all the data member and the member function of a parent class are available to the child class, if they are not private.
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking");
}
public static void main(String args[])
{
Dog d = new Dog();
d.eat();
d.bark();
}
}
output: eating
barking
👉 : By default, all the data member and the member function of a parent class are available to the child class, if they are not private.
2) Multilevel Inheritance
For example:
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking");
}
}
class SmallDog extends Dog
{
void weep()
{
System.out.println("weeping");
}
public static void main(String args[])
{
SmallDog s =new SmallDog();
SmallDog s =new SmallDog();
s.eat();
s.bark();
s.weep();
}
}
output: eating
barking
weeping
3) Multiple Inheritance
In Multiple inheritance one class extending more than one class. Multiple inheritance is not supported in java through the concept of class, but multiple inheritance is supported in java through the concept of interface. we will learn interface in later.
For example:
For example:
class A
{
void show()
{
System.out.println("class A");
}
}
class B
{
void show()
{
System.out.println("class B");
}
}
class C extends A,B
{
public static void main(String args[])
{
C c1 = new C();
c1.show();//now which show() method would be invoked?
}
}
output: compile time error
explanation: To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
consider a scenario where A, B, and C are three classes, the class C extends class A and B, if class A and B have the same method and you call it from child class object, there will be ambiguity to call method of class A and B. Because compile-time error is better than runtime errors, java throw compile time error if you inherit or extends two classes. so whether you have same or different methods there will we compile time error.
4) Hierarchical Inheritance
In Hierarchical inheritance, one parent class will be inherited by many sub(child classes ) classes.
For example:
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking");
}
}
class Loin extends Animal
{
void roar()
{
System.out.println("roaring");
}
public static void main(String args[])
{
Loin l = new Loin();
Loin l = new Loin();
l.eat();
l.bark();//compile time error
l.roar();
}
}
output: eating
roaring
Read More:
Java Encapsulation with Examples.
Java Abstract Class with Examples.
Java Method Overloading with Examples.
Java Wrapper Class with Examples.
Java Inheritance Programming Interview Questions.
Java Inheritance Interview Questions and Answers.
5) Hybrid Inheritance
Hybrid inheritance is not supported by java through class, but it is supported by interface concept.Read More:
Java Encapsulation with Examples.
Java Abstract Class with Examples.
Java Method Overloading with Examples.
Java Wrapper Class with Examples.
Java Inheritance Programming Interview Questions.
Java Inheritance Interview Questions and Answers.