Output of Java program | Set 15 (Inner Classes)

Prerequisite :- Local inner classes , anonymous inner classes

1) What is the output of the following java program?

public class Outer 
{
	public static int temp1 = 1;
	private static int temp2 = 2;
	public int temp3 = 3;
	private int temp4 = 4;
	
	public static class Inner
	{
		private static int temp5 = 5;
		
		private static int getSum()
		{
			return (temp1 + temp2 + temp3 + temp4 + temp5);
		}
	}
	
	public static void main(String[] args)
	{
		Outer.Inner obj = new Outer.Inner();
		System.out.println(obj.getSum());
	}
	
}

a) 15
b) 9
c) 5
d) Compilation Error

Ans. (d)
Explanation: static inner classes cannot access non-static fields of the outer class.

2) What is the output of the following program?


public class Outer 
{
	private static int data = 10;
	private static int LocalClass()
	{
		class Inner
		{
			public int data = 20;
			private int getData()
			{
				return data;
			}
		};
		Inner inner = new Inner();
		return inner.getData();
	}
	
	public static void main(String[] args)
	{
		System.out.println(data * LocalClass());
	}
}

a) Compilation error
b) Runtime Error
c) 200
d) None of the above

Ans. (c)
Explanation: LocalClass() method defines a local inner class. This method creates an object of class Inner and return the value of the variable data that resides within it.

3) What is the output of the following program?

interface Anonymous
{
	public int getValue();
}
public class Outer 
{
	private int data = 15;
	public static void main(String[] args)
	{
		Anonymous inner = new Anonymous()
				{
					int data = 5;
					public int getValue()
					{
						return data;
					}
					public int getData()
					{
						return data;
					}
				};
		Outer outer = new Outer();
		System.out.println(inner.getValue() + inner.getData() + outer.data);
	}
}

a) 25
b) Compilation error
c) 20
d) Runtime error

Ans. (b)
Explanation: the method getData() is undefined in Anonymous class which causes the compilation error.

4) What is the output of the following java program?

public class Outer
{
	private int data = 10;
	
	class Inner
	{
		private int data = 20;
		private int getData()
		{
			return data;
		}
		public void main(String[] args)
		{
			Inner inner = new Inner();
			System.out.println(inner.getData());
			
		}
	}
	private int getData()
	{
		return data;
	}
	public static void main(String[] args)
	{
		Outer outer = new Outer();
		Outer.Inner inner = outer.new Inner();
		System.out.printf("%d", outer.getData());
		inner.main(args);
	}
}

a) 2010
b) 1020
c) Compilation Error
d) None of these

Ans. (c)
Explanation: Inner class defined above though, have access to the private variable data of the Outer class, but declaring a variable data inside an inner class makes it specific to the Inner class with no conflicts in term of variable declaration. For more see Shadowing

5) What is the output of the following program?

interface OuterInterface
{
	public void InnerMethod();
	public interface InnerInterface
	{
		public void InnerMethod();
	}
}
public class Outer implements OuterInterface.InnerInterface, OuterInterface
{
	public void InnerMethod()
	{
		System.out.println(100);
	}
	
	
	public static void main(String[] args)
	{
		Outer obj = new Outer();
		obj.InnerMethod();
	}
}

a) 100
b) Compilation Error
c) Runtime Error
d) None of the above

Ans. (a)
Explanation: Nested Interfaces are defined in java. As both the interfaces has declaration of InnerMethod(), implementing it once works for both the InnerInterface and OuterInterface.

This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.