• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access Modifier question

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


The above code will give compilation error at line 7,8.
Can anyone explain why?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi shetal

http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

link should help understanding why line 7 and 8 throws compile time error.

say() method is proected in the Super class so the class in Package 2 "does" inherit it AND we can call it directly on the SubTwo object via s2.say() or some other method in SubTwo can call say() directly BUT we can't refer to say() on references s or s1 as say() ...

Regards
Maulin
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when an method is declared protected ,it can be accessed only through inheritance since out of the package an protected method is avaliable only to its subclasses.
 
shetal bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanxs..!!
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused now...

Since SubTwo is in package other than that of Super, it cant access protected members of Super. If I will make their package same, then it wil compile. So then what is the difference between default access and protected access. Isnt the protected access behaving as default access here...?
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A default member may be accessed only if the class accessing the member belongs to the same package,whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.
[ June 24, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's the difference between default access and protected access:
-----------------------------------------------------------------

Default access: accessible to classes inside the same package.

Protected access: accessible to classes inside the same package and subclasses in other packages.There's one additional rule for protected access.The subclasses which are in other packages cannot access the class through the class's reference.They have to define a reference of their own type(subclass type) and use it to call the superclass's protected method
 
Rupak Khurana
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"The subclasses which are in other packages cannot access the class through the class's reference.They have to define a reference of their own type(subclass type) and use it to call the superclass's protected method "


Whay is that rule in place ? What good does it do ?
reply
    Bookmark Topic Watch Topic
  • New Topic