Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Protected  RSS feed

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Sandeep. I got it!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!