Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
u cant access a protected method from another class which is not the subclass of that.
Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
"Winners don't do different things<br /> They do things differently"
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Sun Certified Java Programmer
Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
'protected' means all classes in the same package (like default) and sub-classes in any package can access the features. But a subclass in another package can access the protected members in the super-class via only the references of subclass or its subclasses. A subclass in the same package doesn't have this restriction. This ensures that classes from other packages are accessing only the members that are part of their inheritance hierarchy.
Can't access protected method finalize in class java.lang.Object. java.lang.Object is not a subclass of the current class.
Sun Certified Java Programmer
Sun Certified Java Programmer
Sun Certified Java Programmer
Protected
The access level specifier protected, which allows the class itself,
subclasses , and all classes in the same package to access the
members. Use the protected access level when it's appropriate
for a class's subclasses to have access to the member, but not
unrelated classes. Protected members are like family secrets--
you don't mind if the whole family knows, and even a few trusted
friends but you wouldn't want any outsiders to know.
To declare a protected member, use the keyword protected.
First, let's look at how the protected specifier affects
access for classes in the same package.
Consider this version of the Alpha class which is now declared
to be within a package named Greek and which has one protected member
variable and one protected method declared in it:
package Greek;
public class Alpha {
protected int iamprotected;
protected void protectedMethod() {
System.out.println("protectedMethod");
}
}
Now, suppose that the class Gamma was also declared to be a member
of the Greek package (and is not a subclass of Alpha). The Gamma class
can legally access an Alpha object's iamprotected member variable
and can legally invoke its protectedMethod:
package Greek;
class Gamma {
void accessMethod() {
Alpha a = new Alpha();
a.iamprotected = 10; // legal
a.protectedMethod(); // legal
}
}
That's pretty straightforward. Now, let's investigate how the
protected specifier affects access for subclasses of Alpha.
Let's introduce a new class, Delta, that derives from Alpha
but lives in a different package--Latin.
The Delta class can access both iamprotected and
protectedMethod, but only on objects of type Delta or its subclasses.
The Delta class cannot access iamprotected or protectedMethod
on objects of type Alpha. accessMethod in the following code
sample attempts to access the iamprotected member variable on
an object of type Alpha, which is illegal, and on an object of
type Delta, which is legal. Similarly, accessMethod attempts
to invoke an Alpha object's protectedMethod which is also illegal:
package Latin;
import Greek.*;
class Delta extends Alpha {
void accessMethod(Alpha a, Delta d) {
a.iamprotected = 10; // illegal
d.iamprotected = 10; // legal
a.protectedMethod(); // illegal
d.protectedMethod(); // legal
}
}
If a class is both a subclass of and in the same package as
the class with the protected member, then the class has access
to the protected member.
Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
Test 094, IBM WID 6.0 cert
SCJP 1.2
SCBCD 1.3 Beta
SCWCD 1.4 Beta
SCMAD Beta
SCDJWS Beta
KS
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
|