File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unable to comprehend ......in Khalid Mughal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unable to comprehend ......in Khalid Mughal" Watch "Unable to comprehend ......in Khalid Mughal" New topic
Author

Unable to comprehend ......in Khalid Mughal

Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
Hi all, ////////////static synchronized method???

A protected can member can't be accessed by the object ref. of class contating it, instead its accessed by sub class type ref assuming that the subclass in not in the same package..........

To explain this Khalid Mughal gives one example Page 140-141)


then to expalin (4)&(5) he says:

Hence access to protected members at lines (4) and (5) is not permitted as these are not members of an object that can be guaranteed to be implemented by the code accessing them.


I didn't understand the portion in bold. Can someone help me understand it??

i'll really gr8ful

thanx
amit
Shivakanth Thyagarajan
Ranch Hand

Joined: Mar 28, 2005
Posts: 41
Hi,
I didn't understand what were you trying to ask for. I presume this could be your doubt about "Protected access control".

Protected variables or members are visible to all the classes in the same package and to all the classes that inherits the class ( Holding protected variables or methods. )


let's know consider two packages.

package A;

public class A {
protected void methodA( ) {
}
}

class B{

A a = new A( ) ;
void methodB( ) {
a.methodA() ; // This perfectly legal as the classes within the
} // package can access the protected methods.
}


package B ;

class C extends A {
public void methodC ( ){
this.methodA( ) ;
}
} // This is perfectly legal all the classes that inherit A could access the protected methods and variable.

if we could change the above class as,

class C extends A {
A a = new A( ) ;
public void methodC( ) {
a.methodA( ) ;
}
}

The above code would not compile. This throws up the error. Since the protected variables could be accessed only through inheritance or only by the classes that is within the same package.

I hope you would either understand or would have got confussed deeper. Any way is an advantage position for you. You will probe further.

Thanks
Shivakanth.T
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
SubclassB objRrefB = new SubclassA(); //(1)

i think the above code is also wrong as how can subclass reference variable hold the object of another subclass of its superclass they are not related at all...
so compiler error must be generated there also ?

pls comment and explain me the correct thing...


Thanks and Regards, Amit Taneja
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hi all,

hi shivakanth/amit,

well Shiv......to answer ur ques, i'll simply say that i understand what is the meaning of "protected" is, i just wanted to understand what is the meaning or how should i comprehend that portion in bold:
Originally be me:
Hence access to protected members at lines (4) and (5) is not permitted as these are not members of an object that can be guaranteed to be implemented by the code accessing them.
.

just help me undersatnd what is meaning of portion in BOLD.

thanx
amit
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
As amit pointed out, there is an error in your code above. Before the lines you don't understand (the ones marked 4 and 5), there is no mention of objRefA, and in the line marked 1, you make an illegal declaration. A SubclassB is not a SubclassA. The way it stands now, none of the 5 lines are ok, because it won't even compile.
swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
shivakant i am not able to understand one thing in the code.

package A;

public class A {
protected void methodA( ) {
}
}

class B{

A a = new A( ) ;
void methodB( ) {
a.methodA() ; // This perfectly legal as the classes within the
} // package can access the protected methods.
}


package B ;

class C extends A {
public void methodC ( ){
this.methodA( ) ;
}
} // This is perfectly legal all the classes that inherit A could access the protected methods and variable.

if we could change the above class as,

class C extends A {
A a = new A( ) ;
public void methodC( ) {
a.methodA( ) ;
}
}here we are making the new object of the class A. as amit has mentioned
protected member can't be accessed by the object ref. of class contating it.but we are making object of the class.and class c is subclass of class A
then why it will give error.?
please explain this?
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Look at the updated part of the code below

Now you can see that 1) shouldn't have given any compilation error as the Object which is going to access is of type SubClassB and so no probs.
But when u see 2) u can find the problem, at runtime its actually calling SuperClass method of SuperClass's object which is not permissible.

So by looking at these statements you can say that superclassMethodA() and superclassVarA are not guaranteed to be implemented by the reference accessing them, as in 1) it can be true while in case 2) its false.
This is what i think the main intent behind the statement

Hope i am clear
Let me know if i am wrong anywhere.
[ May 24, 2005: Message edited by: Animesh Shrivastava ]
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
You still haven't fixed the problem with line 1. I don't even see that you've changed anything.

You are still trying to cast a SubclassA into a SubclassB.

Your class hierarchy looks like this:

SuperclassA
SubclassA
SubclassB

So, SubclassA is a SuperclassA and SubclassB is also a SuperclassA

However, a SuperclassA is not necessarily a SubclassA, so you can't say

SubclassB objRrefB = new SubclassA(); //(1)


I think, though that if you were to change it to
SubclassB objRrefB = new SubclassB(); //(1)

it would have the functionality explained. The problem is not that it is not guaranteed to implement it, but that you can't see it. You are in a different package, so anything declared with protected scope is invisible to you. The reason you can call it on the instance named objRefB is because of inheritance. If a method/field is declared to be protected, it is visible to subclasses.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Exactly Marks,
U r very much right, i think Amit has posted a wrong code, what u have done is correct. And whatever u have explained is also right.
I just explained the intent of the statement which also can be explained the way i have explained.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to comprehend ......in Khalid Mughal