Table of Contents
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
Terms used in Inheritance
- Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
- Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
- Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
- Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
Learn to code from industry experts! Enroll here!
Types of inheritance in java
1: What is the default value of a boolean in Java?
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
Single Inheritance
When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.
File: TestInheritance.java
classAnimal{
void eat(){System.out.println(“eating…”);}
}
class Dog extends Animal{
voidbark(){System.out.println(“barking…”);}
}
classTestInheritance{
public static void main(String args[]){
Dog d=newDog();
d.bark();
eat();
}}
Output:
barking…eating…
Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.
File: TestInheritance2.java
classAnimal{
void eat(){System.out.println(“eating…”);}
}
class Dog extends Animal{
voidbark(){System.out.println(“barking…”);}
}
classBabyDog extends Dog{
void weep(){System.out.println(“weeping…”);}
}
class TestInheritance2{
publicstatic void main(String args[]){
BabyDog d=new BabyDog();
weep();
d.bark();
eat();
}}
Output:
weeping…barking…eating…
Learn to code from industry experts! Enroll here!
Hierarchical Inheritance
When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.
File: TestInheritance3.java
classAnimal{
void eat(){System.out.println(“eating…”);}
}
class Dog extends Animal{
voidbark(){System.out.println(“barking…”);}
}
classCat extends Animal{
void meow(){System.out.println(“meowing…”);}
}
class TestInheritance3{publicstatic void main(String args[]){
Cat c=new Cat();
meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
meowing…eating…
Multiple Inheritance
Java does not support multiple inheritance. Multiple inheritance means a class derived from more than one direct super class. This increases complexities and ambiguity in the relationship among classes. The problem is clearly visible if we consider what happens in function overriding. Suppose there are two classes, A and B, each defining a function called func(). Now, let’s say we define another class, C, which inherits both from A and B (multiple inheritance), but let’s say this class does not override the function called func().
Now, if we do something like the following:
C c = new C();c.func();
Can we determine which member function func() is invoked? Is it function defined by class A or class B? As we can see, the class C inherits this function doubly from both A and B. This creates ambiguity and the compiler is in a fix to resolve the issue. There is solution to this problem, but it’s extremely complex; therefore, Java decided to stay away from the cause of the problem by not allowing multiple inheritance.
Multiple Inheritance with Interfaces
Although Java does not allow multiple inheritance, it allows a multiple implementation of interfaces. So, in a way the idea is still there. Now, what is an interface?
An interface describes a set of methods but does not provide any concrete implementation for all methods. We can implement the interface with the help of a class which provides the concrete implementation of the method. In this way, a class can implement more than one interface and therefore can provide a concrete implementation of methods derived from one or more interfaces. The class that implements one or more interfaces forms a is-a relationship with the interface type. This also means that objects instantiated from the class are guaranteed to provide the functionality declared by the interface. Any subclass derived from this class also provides the same functionality.
Interfaces are especially useful for providing a common functionality to a number of possibly unrelated classes. Therefore, the object of classes that implement same interface can respond to method calls described in all of the interfaces.
From Java 8, interfaces support default methods with its full implementation. As we know, a class can implement more than one interface; therefore, if multiple interfaces contain a default method with the same method signature, the implemented class should specify which particular method to use or override.
Example
package com.mano.examples;
public interface Interface1 {
default void func(){
System.out.println(“from interface 1”);
}}
package com.mano.examples;public interface Interface2 {
default void func(){
System.out.println(“from interface 2”);
}}
package com.mano.examples;public class MyClass implements Interface1, Interface2 {
public void func(){ // Invoking Interface1 func() method
Interface1.super.func(); // Invoking Interface2 func() method Interface2.super.func();
}
public static void main(String[] args){
MyClass c=new MyClass(); c.func();
}
Learn Coding in your Language! Enroll Here!
Why is it important to choose Entri?
- Excellent online platform for all the Competitive Exams.
- Provides updated materials created by the Entri Experts.
- Entri provides a best platform with full- length mock tests including previous year question papers.
- You can download the app for free and join the required classes.
- Entri wishes you all the best for your examinations and future endeavours.
“YOU DON’T HAVE TO BE GREAT TO START, BUT YOU HAVE TO START TO BE GREAT.”