Inheritance
"Inheritance is a process by which object of one class acquires the properties of the object of another class".
The Mechanism of deriving a new class from an old one is called inheritance.
Types Of Inheritance
1.single Inheritance
2.Multi-level Inheritance
3.Multiple Inheritance
4.Hierarchical Inheritance
5.Hybrid Inheritance
1.single Inheritance:
Only one base class and only one derived class in a program are known as single inheritance.
2.Multi-level Inheritance:
One class inherit into second class and second class inherits into a third class is known as multi-inheritance.
3.Mult Inheritance:
Multiple inheritances do not support in java.
It is not possible in java
4.Hierarchical Inheritance:
More than one class derived from one based class is called hierarchical inheritance.
5.Hybrid Inheritance:
Hybrid Inheritance does not support in java
It is also not possible in java.
-------------------------------------------------------------------------------------------------------
→"extends" is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.
→ Inheritance in java is a mechanism in which one object acquires all the properties and behavior of parent class.
→ We can inherit class using "extends" keyword.
Syntaxclass Super
{
.....
.....
}
class Sub extends Super {
.....
.....
}
Why use inheritance in java
- For Method Overriding so, runtime polymorphism can be achieved.
- For Code Reusability.