File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mock test question on protected access specifier

 
Naresh Gunda
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
Posts: 144
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

/* 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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vipin Das
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() );
}
}
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic