- Hierarchical Inheritance:
- Hybrid Inheritance:
In Hierarchical Inheritance, one class is inherited by many sub classes.
As per above example, Class B, C, and D inherit the same class A.
Hybrid inheritance is a combination of Single and Multiple inheritance.
As per above example, all the public and protected members of Class A are inherited into Class D, first via Class B and secondly via Class C.
Inheritance in Java
- In Java, when an "Is-A" relationship exists between two classes we use Inheritance
- The parent class is termed super class and the inherited class is the sub class
- The keyword "extend" is used by the sub class to inherit the features of super class
- Inheritance is important since it leads to reusability of code
Java Inheritance Syntax:
class subClass extends superClass { //methods and fields }
Java Inheritance Example
class Doctor { void Doctor_Details() { System.out.println("Doctor Details..."); } } class Surgeon extends Doctor { void Surgeon_Details() { System.out.println("Surgen Detail..."); } } public class Hospital { public static void main(String args[]) { Surgeon s = new Surgeon(); s.Doctor_Details(); s.Surgeon_Details(); } }
Super Keyword
The super keyword is similar to "this" keyword.
The keyword super can be used to access any data member or methods of the parent class.
Super keyword can be used at variable, method and constructor level.
0 Comments
If you have any doubts,please let me know