Make delicious recipes!

Java Class Hierarchy - Order of creation



Puzzle: Print the output of the following code.


public class ClassHierarchy {

	static class Foo { 
		public Foo (String s) {System.out.println(s);}
	}
	
	static class Base { 
		private Foo f = new Foo("foo1");
		public Base () {new Foo("foo2"); System.out.println("Base");}   
	}

	static class Derived extends Base { 
		public Derived () {System.out.println("Derived");}
	}
	
	public static void main(String[] args) 
	{
		new Derived ();
	}
}



Output:

foo1
foo2
Base
Derived







Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal