Win a copy of Head First Android this week in the Android forum!
  • 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Doubt in protected modifier

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is mentioned that a subclass outside the package can not access member which has protected modifier using a reference to an instance of superclass..

But below code gives the different behaviour.

package fruit;
public class parent{

protected static void p1( ){
System.out.println("Parent class");
}
}

import fruit.parent;
public class child extends parent{
public static void main (String ARGS[]){
child c = new child();
c.p1();
}
}

class child complies and output : "parent class"

Please explain why it is not giving the compile error.

Thanks in advance.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here you are not accessing the method p1() with a superclass reference. You are accessing it with a subclass reference.
[ September 11, 2006: Message edited by: Keith Lynn ]
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
protected methods may be accessed from any class in the same package and any subclass. Since child is a subclass, it may call the protected method, even though it is not in the same package.




[Clarification: By "and", I did not mean to imply that subclass must be in the same package. I meant that any class in the same package *and* any subclass regardless of package location, may access the protected method.]

Henry
[ September 11, 2006: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you want to challenge the quote that you mentioned, write:



this will not compile.

hope you got it.
[ September 12, 2006: Message edited by: Vaibhav Chauhan ]
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
child c = new child();
c.p1();

please check which type c is.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic