About Question com.enthuware.ets.scjp.v6.2.595 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.2.595 :

Post by ETS User »

Small remark:

Code: Select all

class A
{
	public int i = 10;
	private int j = 20;

}

class B extends A
{
	private int i = 30; //1
	public int k = 40;

}

class C extends B
{
}

public class TestClass
{
	public static void main(String args[])
	{
		C c = new C();
		System.out.println(c.i); //2
		System.out.println(c.j); //3
		System.out.println(c.k); 
	}
}
Explanation:
You cannot access c.i because i is private in B. But you can access ( (A)c).i because i is public in A. Remember that member variables are shadowed and not overridden. So, B's i shadows A's i and since B's i is private, you can't access A's i unless you cast the reference to A.
You cannot access c.j because j is private in b.
I guess You cannot access c.j because j is private in A.

Post Reply

Who is online

Users browsing this forum: No registered users and 220 guests