wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why compile time error??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why compile time error???" Watch "why compile time error???" New topic
Author

why compile time error???

cs singh
Ranch Hand

Joined: Dec 28, 2005
Posts: 36


Answer is: Compile time error.
I'm not getting the reason for it
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Singh,

In class A, the member "i" is marked as protected, so it's accessible only by classes defined in the same package.

If you want to make the "i" member accessible through class B, you must either mark "i" as public or default.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
What Edisandro wrote is not exactly correct.

>> If you want to make the "i" member accessible through class B, you must either mark "i" as public or default.

Default is more restrictive than protected. You will have to declare "i" as public in this example to get it to compile.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
protected members can be accessed from within subclasses of the class in which they are defined.

However, because in the method process in class B, B is not involved in the implementation of an A object, within its methods we cannot refer to the protected members of A.

There is more explanation of this in the Java Language Specification.

http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#62587
ashish kediya
Greenhorn

Joined: Mar 02, 2006
Posts: 15
Originally posted by cs singh:


Answer is: Compile time error.
I'm not getting the reason for it call it as not through a but through b
because it is protected n it inherit as privetly so it can't call by the class where it reside
b know that it's it's own property
ashish kediya
Greenhorn

Joined: Mar 02, 2006
Posts: 15
Originally posted by cs singh:


Answer is: Compile time error.
I'm not getting the reason for it
ashish kediya
Greenhorn

Joined: Mar 02, 2006
Posts: 15
Originally posted by cs singh:


Answer is: Compile time error.
I'm not getting the reason for it
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
I have one doubt, even in other packages, the fields declared as protected will to available to classes which extends that class which has the protected field, how can this will create complie time error???


Kiran Kumar.
Vidhya Hari
Greenhorn

Joined: Mar 20, 2006
Posts: 20
The accessibilty rule of protected is package+kids(subclass)...Which means subclass members can access its protected superclass members only thru inheritance and not via a reference to the superclass...In the above case,since u've accessed the protected i as a.i(via its reference),it gives a compiler error..Try accessing it directly as i(via inheritance)...It should work...
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
The protected members of the superclass are accessible to the subclass only through inheritance i.e instance of subclass.
Not through the super class instances.
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
I totally agree with Thiyanesh. and the protected members are available in any level of inheritance. The visibilty will not come down to private in any level of inheritance.
Nikhil Bansal
Greenhorn

Joined: Mar 21, 2006
Posts: 21
Yes I agree with Thiyanesh.
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
whats Edisandro has written is not correct.

We can access protected members in diffrent packages but only in a subclass (or subclasses or subclass) only through the subclass reference ( and not via superclass reference)
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
I tryed this :
//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.*;
class B extends A //p1.A
{
public void process(A a)
{
a.i = a.i*2;
}
public static void main(String[] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println( a.getI() );
}
}

and it compiles...the problem is with the package...
I think the A that B extends isn't the A from p1.* but another A from p1.p1.*

[ March 21, 2006: Message edited by: Reper Arno ]
[ March 21, 2006: Message edited by: Reper Arno ]

The man who makes no mistakes does not usually make anything<br /> <br />>>> SCJP 5.0 >> SCJD B&S <<< In progress
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Guys,

I don't know what I had in my mind when I wrote this.


In class A, the member "i" is marked as protected, so it's accessible only by classes defined in the same package.

If you want to make the "i" member accessible through class B, you must either mark "i" as public or default.


For sure it's not correct.
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi,

I'm not sure but I think you cannot explicitly access a protected member through an instance variable when you're outside the protected member class's package.

But about one thing I am sure, you can access through inheritance.

I tried to change the process method to ...



Once I ignored the (a) argument, I'm not using it in anywhere else, of course that the result is not the same as expected from the original code but this new one proves that you cannot access a protected member through an instance variable when you're outside the protected member class's package.

I think it makes sense.

Any comments are welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why compile time error???
 
Similar Threads
protected variable?
Question from JQPlus
How does this work ?
protected can not be accessed in derived ??
Declaration and scoping