Example 3
Home - About Us
Inheritance and References Example 1 Example 2 Example 3
  1. public class Demo
    {	
    	public static void main(String args[])
    	{
    		System.out.println("Starting App!");
    	}
    }
    
  2. public class Demo
    {	
    	public static void main(String args[])
    	{
    		System.out.println("Starting App!");
    		Alpha aref = new Alpha();
    		aref.pr();
    	}
    }
    
    class Alpha
    {
    	int a = 10;
    	
    	public void pr()
    	{
    		System.out.println("a: " + a);
    	}
    }
    
  3. public class Demo
    {	
    	public static void main(String args[])
    	{
    		System.out.println("Starting App!");
    		Alpha aref = new Alpha();
    		aref.pr();
    		
    		Beta bref = new Beta();
    		bref.show();			// Legal?  If so, what output?
    		bref.pr();			// Legal?  If so, what output?
    	}
    }
    
    class Alpha
    {
    	int a = 10;
    	
    	public void pr()
    	{
    		System.out.println("a: " + a);
    	}
    }
    
    class Beta extends Alpha
    {
    	int b = 22;
    	
    	public void show()
    	{
    		System.out.println("b: " + b);
    	}
    }
    
  4. public class Demo
    {	
    	public static void main(String args[])
    	{
    		System.out.println("Starting App!");
    		Alpha aref = new Alpha();
    		aref.pr();
    		
    		Beta bref = new Beta();
    		bref.show();				// Legal?  If so, what output?
    		bref.pr();					// Legal?  If so, what output?
    		
    		Gamma gref = new Gamma();
    		gref.pr();    // Legal?  If so, what output?
    		gref.top();  // Legal?  If so, what output?
    		gref.show();  // Legal?  If so, what output?
    		gref.other(100);  // Legal?  If so, what output?
    		gref.other();  // Legal?  If so, what output?
    		
    		Alpha a2 = (Alpha) new Gamma();
    		a2.pr();    // Legal?  If so, what output?
    		a2.top();  // Legal?  If so, what output?
    		a2.show();  // Legal?  If so, what output?
    		a2.other(100);  // Legal?  If so, what output?
    		a2.other();  // Legal?  If so, what output?	
    	}
    }
    
    class Alpha
    {
    	int a = 10;
    	
    	public void pr()
    	{
    		System.out.println("a: " + a);
    	}
    
    	public void top()
    	{
    		System.out.println("top in a: " + a);
    	}
    }
    
    class Beta extends Alpha
    {
    	int b = 22;
    	
    	public void show()
    	{
    		System.out.println("b: " + b);
    	}
    }
    
    class Gamma extends Beta
    {
    	int g = 35;
    	
    	public void pr()
    	{
    		System.out.println("g: " + g);
    	}
    	
    	public void other(int x)
    	{
    		System.out.println("Other: " + x);
    	}
    }
    
  5. public class Demo
    {	
    	public static void main(String args[])
    	{
    		System.out.println("Starting App!");
    		
    		Gamma gref = new Gamma();
    		gref.meth();
    	}
    }
    
    class Alpha
    {
    	int y = 100;
    	int z = 1;
    	
    	public void meth()
    	{
    		System.out.println("Alpha: " + y + ", " + z);
    	}
    }
    
    class Beta extends Alpha
    {
    	int z = 2;
    	
    	public void meth()
    	{
    		System.out.println("Beta: " + y + ", " + z);
    	}
    }
    
    class Gamma extends Beta
    {
    	int y = 300;
    	int z = 3;
    	
    	public void meth()
    	{
    		System.out.println("Gamma: " + y + ", " + z);
    		
    		System.out.println("Gamma: " + y + ", " + super.z);
    		
    		System.out.println("Gamma: " + super.y + ", " + z);
    		
    		System.out.println("Gamma: " + this.y + ", " + this.z);
    		
    		System.out.println("Gamma: " + super.y + ", " + super.z);
    		
    		System.out.println("Gamma: " + y + ", " + ((Alpha) this).z  );
    		
    		System.out.println("Gamma: " + ((Beta) this).y + ", " + super.z);
    	}
    }
    
  6. public class Demo
    {	
    	public static void main(String args[])
    	{
    		System.out.println("Starting App!");
    		
    		Gamma gref = new Gamma();
    		gref.meth();
    		
    		Alpha aref = (Alpha) gref;
    		Beta bref = (Beta) gref;
    		
    		System.out.println("Accessing Alpha ref z: " + aref.z);
    		System.out.println("Accessing Beta ref z: " + bref.z);
    		System.out.println("Accessing Gamma ref z: " + gref.z);
    		
    		System.out.println("Accessing Alpha ref meth: ");
    		aref.meth();
    		System.out.println("Accessing Beta ref meth: ");
    		bref.meth();
    		System.out.println("Accessing Gamma ref meth: ");
    		gref.meth();
    	}
    }
    
    class Alpha
    {
    	int y = 100;
    	int z = 1;
    	
    	public void meth()
    	{
    		System.out.println("Alpha: " + y + ", " + z);
    	}
    }
    
    class Beta extends Alpha
    {
    	int z = 2;
    	
    	public void meth()
    	{
    		System.out.println("Beta: " + y + ", " + z);
    	}
    }
    
    class Gamma extends Beta
    {
    	int y = 300;
    	int z = 3;
    	
    	public void meth()
    	{
    		System.out.println("Gamma: " + y + ", " + z);
    	}
    }
    
  7. public class Demo
    {	
    	public static void main(String args[])
    	{
    		System.out.println("Starting App!");
    		
    		Gamma gref = new Gamma();
    		gref.testing();
    	}
    }
    
    class Alpha
    {
    	int y = 100;
    	int z = 1;
    	
    	public void meth()
    	{
    		System.out.println("Alpha: " + y + ", " + z);
    	}
    }
    
    class Beta extends Alpha
    {
    	int z = 2;
    	
    	public void meth()
    	{
    		System.out.println("Beta: " + y + ", " + z);
    	}
    }
    
    class Gamma extends Beta
    {
    	int y = 300;
    	int z = 3;
    
    	public void testing()
    	{
    		this.meth();
    		super.meth();
    		((Alpha) this).meth();
    		
    		System.out.println("-----------------------");
    		
    		Alpha a2 = (Alpha) this;
    		Beta b2 = (Beta) this;
    		Gamma g2 = (Gamma) this;
    		
    		a2.meth();
    		b2.meth();
    		g2.meth();
    	}
    
    	public void meth()
    	{
    		System.out.println("At start of Gamma: " + y + ", " + z);
    //		super.meth();
    //		((Alpha) this).meth();	
    	}
    }
    
    
Home - About Us
Copyright © 2006 by Kiowok, Ann Arbor, Michigan, USA