Inheritance in Java OOPs with Example 2 (JNNC Technologies)


  1. Hierarchical Inheritance:
  2. In Hierarchical Inheritance, one class is inherited by many sub classes.
    Types of Inheritance
    As per above example, Class B, C, and D inherit the same class A.
  3. Hybrid Inheritance:
  4. Hybrid inheritance is a combination of Single and Multiple inheritance.
    Types of 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

Concept of Inheritance Java and Java Polymorphism
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.

Inheritance in Java OOPs with Example

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();