Re: How to correctly by pass "protected"

From: Roland J Rankin Jr (mostlyspam_at_placebocode.com)
Date: 01/18/05


Date: Tue, 18 Jan 2005 16:29:29 +0000

I think I missed something.. Given the code below..

package com.placebocode.test;
public class Parent
{
     int x = 0;
     public Parent()
     {
     }

     protected int x()
     {
         return this.x;
     }

}
/ ** new class and new Package **/
package com.placebocode.test2;
import com.placebocode.test.Parent;

public class Child
{
     private Child()
     {
     }
     public static int getX(Parent parent)
     {
         return parent.x();
     }

}

/ * End Code */

I get the following error.
com/placebocode/test2/Child.java [13:1] x() has protected access in
com.placebocode.test.Parent
         return parent.x();
                      ^
1 error
Errors compiling Child.

Which I found really surprising. I can change the Child class package
declaration to match the parent and everything works. but if it's in a
different package. It refuses to compile.

I guess I'm back to answer 3a. he he.



Relevant Pages