File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a 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 "a "protected" issue" Watch "a "protected" issue" New topic
Author

a "protected" issue

Kian Ng
Greenhorn

Joined: Dec 18, 2005
Posts: 17
package birdpack

public class Bird
{
protected int n;
}
------------------------------------------------------------------------------
package duckpack
import birdpack.*;

class Duck1
{
void m()
{
n=100;
}
}
//compiles!
----------------------------------------------------------------------------
package duckpack
import birdpack.*;

class Duck2
{
void m()
{
Duck2 d= new Duck2();
d.n=100;
}
}
//compiles!
----------------------------------------------------------------------------
package duckpack
import birdpack.*;

class Duck3
{
void m()
{
Bird b=new Bird();
b.n=100;
}
}
//failed, error: n has protected access in birdpack.Bird
----------------------------------------------------------------------------
package duckpack
import birdpack.*;

class Swan
{
void m()
{
Duck1 b=new Duck1();
b.n=100;
}
}
//failed, error: n has protected access in birdpack.
----------------------------------------------------------------------------

can someone explain why the bottom 2 classes failed to compile?
thanks alot! (headache...)
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Well, I`m not even sure why the Duck1 class compiles if there`s no declaration of member n that is used in void m() method. Shouldn`t class Duck1 inherit from class Bird?

Class Duck3 doesn`t compile because Duck3 and Bird are in different packages and you are not allowed to access protected member of a class from a class in a different package.

The same thing here, I think class Duck1 is supposed to inherit from Bird, because there`s no initialization of member n in Duck1 class. In this case the fourth class wouldn`t compile because the protected member n is declared in Bird, Duck1 just inherit the member n, but since Bird and Swan are in different packages you are not allowed to acces protected member. Place of declaration counts, protected member n is declared outside the package duckpack (it`s declared in Bird and Bird is in birdpack package) so the code won`t compile.
Waseem Haider
Greenhorn

Joined: Jan 05, 2006
Posts: 1
Here are the rules Protected.
1- It behaves just like default access except that it takes into account parent child relationship between classes in two different packages.
2- So you can only access protected members by extending the outside-package-superclass.
3- Even though you have extended the outside-package-superclass and you can access this member as your own member (as given by inheritance definition) you can not access the same member by reference.
4- The protected member which is inherited to the extending class becomes a private member of it. So other classes sharing the package of the extending class does not know about inherited protected member's existence.


fast_learner
Kian Ng
Greenhorn

Joined: Dec 18, 2005
Posts: 17
hey guys i made a mistake there, sorry man, all e classes in duckpack is suppose to "extends" the Bird class. And tat is what makes me so puzzled. arent dervied classes be allow to access the protected variables?
Help... Still puzzled, many thanks!!!
Aleksander Zielinski
Ranch Hand

Joined: Nov 11, 2005
Posts: 127
Read my previous post, I explained almost everything. Here I can add that even if class Duck3 also inherit from Bird you are not allowed to access proteced member of Bird class in Duck3 class, however you can access protected member n of Duck3 class in Bird class. I added inheritation and made some comments to your code.

package birdpack

public class Bird
{
protected int n;
}
----------------------------------------------------------------------------
package duckpack
import birdpack.*;

class Duck1 extends Bird
{
void m()
{
n=100;
}
}

/* compiles because protected members are accessible within the same class and you are not accesing protected member n of Bird class here just inherited member n that belongs to Duck1 class */
----------------------------------------------------------------------------
package duckpack
import birdpack.*;

class Duck2 extends Bird
{
void m()
{
Duck2 d= new Duck2();
d.n=100;
}
}

/* the same thing here, compiles because you are accesing inherited member n in concrete instance of Duck2 class */
----------------------------------------------------------------------------
package duckpack
import birdpack.*;

class Duck3 extends Bird
{
void m()
{
Bird b=new Bird();
b.n=100;
}
}

/* won`t compile because you are trying to access protected member in instance of Bird class */
----------------------------------------------------------------------------
package duckpack
import birdpack.*;

class Swan extends Bird
{
void m()
{
Duck1 b=new Duck1();
b.n=100;
}
}

/* won`t compile because you are trying to access protected member DECLARED in different package, though it might seem that you are trying to access member from the same package (Duck1 and Swan are in the same package) code won`t compile because Duck1 class inherits protected member n from Bird class which is in different package and the place of member declaration counts. */


Hope that helps.
Kian Ng
Greenhorn

Joined: Dec 18, 2005
Posts: 17
thanks guys!!! cos i have always thought tat by extending the superclass with the protected variables/methods, its features can be made available to the subclass and other classes that uses the subclass...
 
wood burning stoves
 
subject: a "protected" issue