GrandparentDemo.java

Download GrandparentDemo.java

 1: // Demo showing overriding, inheritance, and hiding.
 2: //
 3: // Written 11/2011 by Wayne Pollock, Tampa Florida USA
 4: 
 5: class Grandparent {
 6:   int num = 1;
 7:   Grandparent () {
 8:     System.out.println( "Grandparent constructor called." );
 9:   }
10: 
11:   void poly () { System.out.println( "Grandparent.poly called." ); }
12: }
13: 
14: class Parent extends Grandparent {
15:   int num = 2;
16:   Parent () {
17:     System.out.println( "Parent constructor called." );
18:   }
19: 
20:   void poly () { System.out.println( "Parent.poly called." ); }
21: }
22: 
23: class Child extends Parent {
24:   int num = 3;
25:   Child () {
26:     System.out.println( "Child constructor called." );
27:   }
28: 
29:   void poly () { System.out.println( "Child.poly called." ); }
30: }
31: 
32: public class GrandparentDemo
33: {
34:   public static void main ( String [] args ) {
35:    System.out.println( "Creating new Child():" );
36:    Grandparent gp = new Child();
37: 
38:    System.out.println( "\ngp.num = " + gp.num );
39:    System.out.println( "((Child) gp).num = " + ((Child) gp).num );
40: 
41:    System.out.println( "\nInvoking gp.poly():" );
42:    gp.poly();
43:   }
44: }