• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

mock test question on protected access specifier

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() );
}
}
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic