Doubt on protected
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
i have 2 classes :
Class ProtectedTestClass2 :
and
Class ProtectedTestClass1 :
when i compile & run ProtectedTestClass1, it gives me the output :
Why ???
This has really got all my concepts about protected wrong. I thought that a protected member can be accessed only by a subclass in same or different package or by the object of the subclass if it is in the subclass itself.
But now here i stand watching the protected member being used, by the object of the class it is declared in, in some other class within the same package just like the default. Then this means that the only difference between default & protected is that protected can be accessed across different packages by extending the class whereas default only works for the same package.
Please someone look into this explain me why is such a behaviour of protected??
Class ProtectedTestClass2 :
and
Class ProtectedTestClass1 :
when i compile & run ProtectedTestClass1, it gives me the output :
Why ???
This has really got all my concepts about protected wrong. I thought that a protected member can be accessed only by a subclass in same or different package or by the object of the subclass if it is in the subclass itself.
But now here i stand watching the protected member being used, by the object of the class it is declared in, in some other class within the same package just like the default. Then this means that the only difference between default & protected is that protected can be accessed across different packages by extending the class whereas default only works for the same package.
Please someone look into this explain me why is such a behaviour of protected??
Regards,<br />Sourin.
posted 18 years ago
That's is how it is defined. Protected allows access by all subclasses *and* classes within the same package.
Henry
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Please someone look into this explain me why is such a behaviour of protected??
That's is how it is defined. Protected allows access by all subclasses *and* classes within the same package.
Henry
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Sen,
Try this code and be confident what you studied was correct
(but you need to add something to what to understood about
protected).
package com.nihilent.sourin.scjp2; //modified line
import com.nihilent.sourin.scjp1;
public class ProtectedTestClass1 {
public static void main(String[] args) {
ProtectedTestClass2 ptc = new ProtectedTestClass2();
System.out.println("Value of a = " + ptc.a); //compiler error
}
}
}
[/code]
One more tricky fact for exam:
See the code below
One more thing:
Read the code attentively
package com.nihilent.sourin.scjp2; //modified line
import com.nihilent.sourin.scjp1;
public class ProtectedTestClass1 {
public static void main(String[] args) {
ProtectedTestClass2 ptc = new ProtectedTestClass2();
System.out.println("Value of a = " + ptc.a); //compiler error
}
}
}
[/code]
One more tricky fact for exam:
See the code below
See one more example:
Got It Sen?
Regards,
cmbhatt
Try this code and be confident what you studied was correct
(but you need to add something to what to understood about
protected).
package com.nihilent.sourin.scjp2; //modified line
import com.nihilent.sourin.scjp1;
public class ProtectedTestClass1 {
public static void main(String[] args) {
ProtectedTestClass2 ptc = new ProtectedTestClass2();
System.out.println("Value of a = " + ptc.a); //compiler error
}
}
}
[/code]
One more tricky fact for exam:
See the code below
One more thing:
Read the code attentively
package com.nihilent.sourin.scjp2; //modified line
import com.nihilent.sourin.scjp1;
public class ProtectedTestClass1 {
public static void main(String[] args) {
ProtectedTestClass2 ptc = new ProtectedTestClass2();
System.out.println("Value of a = " + ptc.a); //compiler error
}
}
}
[/code]
One more tricky fact for exam:
See the code below
See one more example:
Got It Sen?
Regards,
cmbhatt
cmbhatt
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Essentially, protected is less 'private' than package, so any member that is protected is also accessible as if it were a 'package' variable.
private > 'package' > protected > coderanch.
private > 'package' > protected > coderanch.
ASCII silly question, Get a silly ANSI.
| passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











