Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage?

Reply
Thread Tools

why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage?

 
 
junzhang1983@gmail.com
Guest
Posts: n/a
 
      01-26-2008
package XX;
class A
{
protected void kk ();
}

package YY;
import XX.A;

class B extends A
{

}

package YY;
class C
{
public void zz()
{
kk(); //why can call B.kk()????
}
}



but if we modify as below:
package YY;
import XX.A;

class B extends A
{
protected void kk ()
{
super.kk();
}
}


why above modify can success?
3ks
 
Reply With Quote
 
 
 
 
Hal Rosser
Guest
Posts: n/a
 
      01-26-2008

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> package XX;
> class A
> {
> protected void kk ();
> }
>
> package YY;
> import XX.A;
>
> class B extends A
> {
>
> }
>
> package YY;
> class C
> {
> public void zz()
> {
> kk(); //why can call B.kk()????
> }
> }
>

_________
Because protected keyword gives access to subclasses and other members of
the same package.
And class C is not a subclass and not in package XX.
>
>
> but if we modify as below:
> package YY;
> import XX.A;
>
> class B extends A
> {
> protected void kk ()
> {
> super.kk();
> }
> }
>
>
> why above modify can success?
> 3ks

Because B is a subclass of A


 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      01-26-2008
<(E-Mail Removed)> wrote ...
>> package XX;
>> class A
>> {
>> protected void kk ();
>> }
>>
>> package YY;
>> import XX.A;
>>
>> class B extends A
>> {
>>
>> }
>>
>> package YY;
>> class C
>> {
>> public void zz()
>> {
>> kk(); //why can call B.kk()????
>> }
>> }


This will not compile. Why does the comment refer to B.kk()? Class B is not
involved in the C invocation at all. C does not have a method kk().

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-28-2008
On Fri, 25 Jan 2008 22:33:01 -0800 (PST), http://www.velocityreviews.com/forums/(E-Mail Removed)
wrote, quoted or indirectly quoted someone who said :

>why above modify can success?


two things. Please use standard caps conventions when you write code,
especially lower case package names. See
http://mindprod.com/jgloss/codingconventions.html

It makes your code look extremely weird to an eye accustomed to
conformant code.

Next read up on what the various scopes are supposed to do.

See http://mindprod.com/jgloss/scope.html
http://mindprod.com/jgloss/protected.html
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does perl allow so many different ways of doing essentially thesame thing? Peng Yu Perl Misc 23 06-12-2010 10:09 AM
How can I transform source range to destination range that is thesame as source? Lambda C++ 2 07-16-2008 05:18 PM
Getting NameError when trying to create a form with multiple of thesame object laredotornado Ruby 1 01-28-2008 12:17 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Can't compile (perlcc) both Net::FTP and Config::IniFiles at thesame time Thomas Watson Steen Perl Misc 4 10-04-2004 02:21 AM



Advertisments