This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dans mock exam -Question on protected access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dans mock exam -Question on protected access" Watch "Dans mock exam -Question on protected access" New topic
Author

Dans mock exam -Question on protected access

vijaya dev
Ranch Hand

Joined: Jun 22, 2004
Posts: 33
This Question is taken from Dans Mock exam
***********************************************

// Class A is declared in a file named A.java.
package com.dan.chisholm;
public class A {
public void m1() {System.out.print("A.m1, ");}
protected void m2() {System.out.print("A.m2, ");}
private void m3() {System.out.print("A.m3, ");}
void m4() {System.out.print("A.m4, ");}
}
// Class C is declared in a file named C.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class C extends A {
public static void main(String[] args) {
C c = new C();
c.m1(); // 1
c.m2(); // 2
c.m3(); // 3
c.m4(); // 4
}}

*****************************************
The answer to this question is there is a compiler error in step 3.
How come there isnt error in the step 2 too.
My understanding accordking to the K & B is the subclass can see the protected class through interitance and it cannot use the dot operator (reference)

Please clarify my question.

Thanks,
vijaya


SCJP 1.4
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
The protected method is an instance member and so it will need the dot operator !!
However if the method were static then you cannot use the dot operator.
[ August 14, 2004: Message edited by: Murtuza Akhtari ]

---<br />SCJP 1.4
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Also in addition to the answer

Compile error at line 3

there will also be a compile error at line 4
since method m4() is declared without any modifiers and so will have the default access...which is only for the same package. Since class A and class C are in different packages...invoking method m4() from class C will result in compile error.
vijaya dev
Ranch Hand

Joined: Jun 22, 2004
Posts: 33
I am a bit confused here in Page 78 in K&B the example uses instance member

From K & b

package certificate;
public class Parent {
protected int x=9;
}
package other;
import certificate.Parent{
public void testit()
{
System.out.println(" X is"+ x); Works okay
Parent p=new Parent();
System.out.println(" X is"+ p.x); compiler error

}

Is this right..or Am i getting confused
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Notice the difference here...

In the first case, you are instantiating an object of the child class and in the second case you are instantiating an object of the parent class.

You cannot use a parent reference to access protected members, however you can use the child reference.

Also note on Page 79 K&B book---paragraph right after the error message

...Finally we've seen that subclasses outside the package can't use a superclass reference to access a protected member.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Murtuza Akhtari:
Also in addition to the answer

Compile error at line 3

there will also be a compile error at line 4
since method m4() is declared without any modifiers and so will have the default access...which is only for the same package. Since class A and class C are in different packages...invoking method m4() from class C will result in compile error.


Murtuza,

You are correct and the answer in my exam is consistent with your statement.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Murtuza Akhtari:
Notice the difference here...

In the first case, you are instantiating an object of the child class and in the second case you are instantiating an object of the parent class.

You cannot use a parent reference to access protected members, however you can use the child reference.

Also note on Page 79 K&B book---paragraph right after the error message



Here's an example.

vijaya dev
Ranch Hand

Joined: Jun 22, 2004
Posts: 33
Thanks for your replies...
 
wood burning stoves
 
subject: Dans mock exam -Question on protected access