Skip to main content

Hierarchical Inheritance

Hierarchical inheritance
  • In this type of inheritance property of superclass is inherited to
    more than one sub class
Hierarchical inheritance Example
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
class demo3
   {
      int k=13;
      void test()
      {
    	  System.out.println("running test method");
      }
 
   }
 
  class Sample3 extends demo3
  {
	  double d=12.5;
	  void disp()
	  {
		  System.out.println("running disp method");
	  }
 
  }
 
  class run3 extends demo3
  {
	  boolean b=true;
 
	  void run()
	  {
		  System.out.println("running run method");
	  }
 
  }
 
public class Hierarchical
{
  public static void main(String[] args)
  {
		System.out.println("**************************");
 
		Sample3 obj1=new Sample3();
 
		//sample3 object have property of Sample3 and demo3
		 System.out.println("k value: "+obj1.k);
	         obj1.test();
 
	         System.out.println("d value: "+obj1.d);
	         obj1.disp();
 
		System.out.println("**************************");
 
		run3 obj2=new run3();
 
		//run3 object have property of run3 and demo3
 
		System.out.println("k value: "+obj2.k);
		obj2.test();
 
		System.out.println("b value: "+obj2.b);
		obj2.run();
 
		System.out.println("**************************");
 
 
  }	
 
}
 
output:
 
**************************
k value: 13
running test method
d value: 12.5
running disp method
**************************
k value: 13
running test method
b value: true
running run method
**************************