• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why compile time error???

 
cs singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Answer is: Compile time error.
I'm not getting the reason for it
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
david allen
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by cs singh:


Answer is: Compile time error.
I'm not getting the reason for it
 
ashish kediya
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by cs singh:


Answer is: Compile time error.
I'm not getting the reason for it
 
bnkiran kumar
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Vidhya Hari
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I agree with Thiyanesh.
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic