• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why compile time error???

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Answer is: Compile time error.
I'm not getting the reason for it
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by cs singh:


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

 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I agree with Thiyanesh.
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic