• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic