Java Theory
Inheritance:
The Inheritance is a process of obtaining the data members and methods from one class to another class, plus can have its own is known as inheritance. It is one of the fundamental features of object-oriented programming.
Inheritance - IS-A relationship between a superclass and its subclasses.
Super Class: The class whose features are inherited is known as a superclass (or a base class or a parent class).
Sub Class: The class that inherits the other class is known as a subclass (or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
Diagram:
Inheritance-Real-Life-Example-of-Inheritance
The real-life example of inheritance is child and parents, all the properties of a father are inherited by his son.
Implementation with Example :
Dog class is inheriting behavior and properties of Animal class and can have its own Too.
Example |
---|
public class Inheritance { public static void main(String[] args) { Animal dog = new Dog(); dog.setId(123); // inherited from superclass dog.sound(); // overridden behavior of sub class } } class Animal { int id; public int getId() { return id; } public void setId ( int id ) { this.id = id; } } |
The keyword used for inheritance is extends.
Syntax |
---|
class derived-class extends base-class { //Methods and fields } |
Types of Inheritance in Java:
Different types of inheritance which are supported by Java.
1. Single Inheritance:
Single Inheritance
A class inherits another class, it is known as a single inheritance. For example Dog class inherits the Animal class, so there is the single inheritance.
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. Multilevel Inheritance:
Multilevel Inheritance
In Multilevel Inheritance, a derived class inheriting a base class and as well as the derived class also act as the base class to other classes.
3. Hierarchical Inheritance:
Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass.
4. Multiple Inheritance:
Multiple Inheritance
5. Hybrid Inheritance:
Hybrid Inheritance
It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritances with classes, the hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.
Why is multiple inheritance not supported in java?
1. To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
2. Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from a child class object, there will be ambiguity to call the method of A or B class.
3. Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have the same method or different, there will be a compile time error.
Example |
---|
class A { void msg(){System.out.println("Hello");} } class B { void msg(){System.out.println("Welcome");} } class C extends A,B{//suppose if it were} Public Static void main(String args[]) { C obj=new C(); obj.msg();//Now which msg() method would be invoked? } |