/home/wpollock1/public_html/Java/GrandparentDemo.java
// Demo showing overriding, inheritance, and hiding.
//
// Written 11/2011 by Wayne Pollock, Tampa Florida USA
class Grandparent {
int num = 1;
Grandparent () {
System.out.println( "Grandparent constructor called." );
}
void poly () { System.out.println( "Grandparent.poly called." ); }
}
class Parent extends Grandparent {
int num = 2;
Parent () {
System.out.println( "Parent constructor called." );
}
void poly () { System.out.println( "Parent.poly called." ); }
}
class Child extends Parent {
int num = 3;
Child () {
System.out.println( "Child constructor called." );
}
void poly () { System.out.println( "Child.poly called." ); }
}
public class GrandparentDemo
{
public static void main ( String [] args ) {
System.out.println( "Creating new Child():" );
Grandparent gp = new Child();
System.out.println( "\ngp.num = " + gp.num );
System.out.println( "((Child) gp).num = " + ((Child) gp).num );
System.out.println( "\nInvoking gp.poly():" );
gp.poly();
}
}