Thursday, 23 February 2017

What is Hierarchical Inheritance In Java ?

Hierarchical Inheritance :

Java supports Hierarchical Inheritance. In Hierarchical Inheritance there is one super class and many sub classes. for example below figure shows Hierarchical classification of accounts in bank. This is possible because all the accounts have some certain common features. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Account{ //Parent Class
 void accountMinimumBalance(){
  System.out.println("ACCOUNT MINIMUM BALANCE RS 0/-");
 }
}
class Saving extends Account { // Sub Class of Account 
 void getSavingMinimumBalance(){
  System.out.println("SAVING ACCOUNT MINIMUM BALANCE RS 10/-");
 }
}
class FixedDeposite extends Account { // Sub Class of Account
 void getFixedDepositeMinimumBalance(){
  System.out.println("FIXED-DEPOSITE ACCOUNT MINIMUM BALANCE RS 20/-");
 }
}
class Current extends Account { // Sub Class of Account
 void getCurrentMinimumBalance(){
  System.out.println("CURRENT ACCOUNT MINIMUM BALANCE RS 30/-");
 }
}
class Short extends FixedDeposite { // Sub Class of FixedDeposite
 void getShortMinimumBalance(){
  System.out.println("SHORT ACCOUNT MINIMUM BALANCE RS 40/-");
 }
}
class Medium extends FixedDeposite { // Sub Class of FixedDeposite
 void getMediumMinimumBalance(){
  System.out.println("LONG ACCOUNT MINIMUM BALANCE RS 50/-");
 }
}
class Long extends FixedDeposite{ // Sub Class of FixedDeposite
 void getLongMinimumBalance(){
  System.out.println("MEDIUM ACCOUNT MINIMUM BALANCE RS 60/-");
 }
}
public class HierarchicalInheritanceExample {
 public static void main(String[] args) {
  Account a = new Account();
  a.accountMinimumBalance();
  Saving b = new Saving();
  b.getSavingMinimumBalance();
  FixedDeposite c = new FixedDeposite();
  c.getFixedDepositeMinimumBalance();
  Current d = new Current();
  d.getCurrentMinimumBalance();
  Short e = new Short();
  e.getShortMinimumBalance();
  Medium f = new Medium();
  f.getMediumMinimumBalance();
  Long g = new Long();
  g.getLongMinimumBalance();
 }
}
Output:

ACCOUNT MINIMUM BALANCE RS 0/-
SAVING ACCOUNT MINIMUM BALANCE RS 10/-
FIXED-DEPOSITE ACCOUNT MINIMUM BALANCE RS 20/-
CURRENT ACCOUNT MINIMUM BALANCE RS 30/-
SHORT ACCOUNT MINIMUM BALANCE RS 40/-
LONG ACCOUNT MINIMUM BALANCE RS 50/-
MEDIUM ACCOUNT MINIMUM BALANCE RS 60/-



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment