aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in  protected access specifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in  protected access specifier" Watch "doubt in  protected access specifier" New topic
Author

doubt in protected access specifier

wali nagesh
Greenhorn

Joined: Dec 26, 2008
Posts: 2
how is protected access specifier used in a subclass outside a package?
how about using parent class reference in a child class to access protected variable?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
wali nagesh wrote:how is protected access specifier used in a subclass outside a package?
how about using parent class reference in a child class to access protected variable?


Protected is only inherited by subclasses, so you can only use protected member using subclass reference outside a package.

Remember one thing leaving public access modifier, anything outside a package is considered as private, so you cannot call anything without public modifier using superclass references.

Do you need examples or you are clear now?


SCJP 6
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
The access modifiers proceed from least restrictive to most restrictive thus:

public protected [default] private

As has been said, protected class members are inherited by subclasses even outside of the package
in which they are defined.

But subclasses outside of the package can only use these members via inheritance--and NOT by
reference--not by using an instance of the class they are defined in:

//pseudo code

package A; class A1 { protect int x = 7; ... }

package B; class B1 extends A1 { A1 myA = new A1(); void myMeth() { System.out.println(x); } // prints 7

//NOT ----------------> void myMeth() { System.out.println(myA.x); }
//end pseudo code

It's an important distinction that you may be tested on.







SCJP 88% | SCWCD 84%
 
Don't get me started about those stupid light bulbs.
 
subject: doubt in protected access specifier