• 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

Protected

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
The answer given for the following code is that the error occurs at lines 3 and 4. I feel that line 2 should be included since protected methods are accessible only thro inheritance and not by creating an instance of class.Am I rite?can somebody pl help?Thanks


(CODE TAGS ADDED TO MAKE IT MORE READABLE)
[ May 15, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Accessibility to protected members of superclass is permitted via references of subclasses (and not reference of its superclass). In a nutshell, protected members of a class are accessible by any class in the same package and by those subclasses of its class in other packages. Hope this helps.

Sandeep
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you compile the two classes?
 
Sandeep Advani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello:

m3() will have private access in A

m4() is not public in A and cannot be accessed from outside the package.

Is this what you are looking for ?

Sandeep
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Sandeep, I'm asking Sridhar the question.
 
Sandeep Advani
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad. The error I explained above was from Sridhar's code. I should have mentioned that.

Now what I said before is explained better in following code.

package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class C extends A {
C c1 = new C();
public static void main(String[] args) {
C c = new C();
c.m1(); // 1
c.m2(); // 2
//c.m3(); // 3
//c.m4(); // 4
}

void m5(A objRefA) {
c1.m2();
objRefA.m2(); //m2() has protected access and cannot be referenced from its superclass. Only by its subclasses. (Different packages are assumed.)
}
}

I am only trying to focus on m2() which is protected.

Thanks
Sandeep
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sandeep. I got it!
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic