aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mock test question on protected access specifier 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 "mock test question on protected access specifier" Watch "mock test question on protected access specifier" New topic
Author

mock test question on protected access specifier

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
//in file A.java
package p1;
public class A
{
protected int i = 10;
public int getI() { return i; }
}
//in file B.java
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i = a.i*2; //line 1
}
public static void main(String[] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println( a.getI() );
}
}

Result: Compilation error, i has protected access in p1.A
//line 1 is giving error

/* A protected member is accessible in all classes in the package containing its class, and by all subclasses of its class in any package where this class is visible. */

here, B is the subclass of A. Then why this programe is giving compilation error? can any one of you please explain me.

Thanx in advance
Naresh
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144

/* A protected member is accessible in all classes in the package containing its class, and by all subclasses of its class in any package where this class is visible. */



Yep, a protected member is acessible in the subclass but in a slightly different way. You cannot invoke a protected member of a super class from its sublclass (assuming the subclass is in a different package) using an instance of the super class. The subclass inherits the protected member and hence can access the protected member the inheritance way.

Your code will compile if you replace a.i = a.i*2 by i=i*2.


Arvind
Jan Valenta
Ranch Hand

Joined: Nov 30, 2005
Posts: 32
Hi,

protected members and methods can only be accessed in the direct super-instance. I mean you can use member variable 'i' in 'b' (which is in fact b.super.i), but you can not use variable a.i, as 'a' is not direct "super-instance" of 'b'. Huh, hope it's clear. :-)

Jan


Anybody knows about a job for <a href="http://cern.ch/jvalenta/valenta-cv.pdf" target="_blank" rel="nofollow">SCJP5.0, Ph.D. in Software Engineering, 2 years Java, C, C++</a> in Montreal, QC, Canada? Thanks!
Vipin Das
Ranch Hand

Joined: Jul 05, 2004
Posts: 47
Hi
" public class B extends p1.A" with this statement u r inheriting the variable i. So u can directly access i in B. But u r accessing it through the object of A. Inside A it still has protected access.

Thanks and regards,
Vipin C. Das
praveen Shangunathan
Ranch Hand

Joined: Sep 06, 2005
Posts: 65
apart from inheritence, you can use a subclass reference to access protected members of the superclass, like
B b = new B();
b.i = b.i*2; //line 1
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
here's the code...running with some changes

//in file A12.java
package p1;
public class A12
{
protected int i = 10;
public int getI() { return i; }
}


//Result: Compilation error, i has protected access in p1.A
//line 1 is giving error

/* A protected member is accessible in all classes in the package containing its class, and by all subclasses of its class in any package
*where this class is visible. */

//here, B is the subclass of A. Then why this programe is
//giving compilation error? can any one of you please explain
//me.

//Thanx in advance
//Naresh

//Solution- at line 12 in file B12.java, you are saying
//A a = new B();. at line 14 you are passing superclass
//reference,to method 6 and using it to access the protected
//variable. This is not allowed.
//See the following code with proper changes

//in file B12.java
package p2;
import p1.*;

public class B12 extends A12
{
B12 b;
public void process(A12 a)
{
b=(B12)a;
b.i = b.i*2; //line 1
}
public static void main(String[] args)
{
A12 a = new B12();
B12 b = new B12();
b.process(a);
System.out.println( a.getI() );
}
}


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: mock test question on protected access specifier