Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Question on access modifiers

Options
  • 09-09-2010 10:43am
    #1
    Registered Users Posts: 2,406 ✭✭✭


    Hi guys. Studying the book "A programmers guide to java scjp" at the moment.

    Question 4.18 on Member Accessibility Modifiers is killing me :(

    Q. Which lines that are marked in the following code will compile?
    *i've compiled the code and displayed the output but am still none the wiser.
    package packageA;
    
    public class A {
    	protected int pf;
    	}
    
    package packageB;
    import packageA.A;
    
    public class B extends A {
    	void Action(A objA, B objB, C objC) {
    		pf = 10;
    		objA.pf = 10; // error 1
    		objB.pf = 10;
    		objC.pf = 10;
    	}
    }
    
    class C extends B {
    	void Action(A objA, B objB, C objC) {
    		pf = 10;
    		objA.pf = 10; // error 2
    		objB.pf = 10; // error 3
    		objC.pf = 10;
    	}
    }
    

    Compilation of Class B fails (as expected) and matches what the book says will happen.
    C:\Users\brianon\Desktop\B.java:7: pf has protected access in packageA.A
    		objA.pf = 10;
    		    ^
    C:\Users\brianon\Desktop\B.java:16: pf has protected access in packageA.A
    		objA.pf = 10;
    		    ^
    C:\Users\brianon\Desktop\B.java:17: pf has protected access in packageA.A
    		objB.pf = 10;
    		    ^
    3 errors
    

    Now I thought I understood public, package-default, private and protected but maybe there is something else I am not understanding.

    Error 1 : pf of Class A. objA IS an instance of class A. Surely it has access to the protected pf ?

    Error 2 : pf of Class A. objA IS an instance of class A. C extends B which extands A. Subclasses have access to protected vars no ?

    Error 3 : pf of Class A. objB IS an instance of class B. B extends A. Subclasses have access to protected vars no ?


    Help would be appreciated. What am I missing ? This is stopping me from moving on as I feel I NEED to know this :(


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Have a look at the following link: http://www.devx.com/tips/Tip/14713

    Read the last paragraph about subclass access through separate packages.

    The access is only permitted on the subclassed type, and of types that derive from that subclassed type. So in the example B and C in the case of when we're in B.

    So since A is not sub-classed that's forbidden. B is derived from A so that's allowed, and C directly is derived from B so that's fine.

    Now in C, the rule for A still holds, and since B isn't derived from C (the type we're currently in) it's forbidden, and then C is allowed since it derives from A.

    If you had another type D that derives from C here, then that would also compile.

    <edit>All of this is only valid when the derived types are in a different packages than the protected attribute (so A in our case). If the types are all in the once package, then everything happens as normal (all sub-types have access to the protected attribute). </edit>


  • Registered Users Posts: 2,406 ✭✭✭brianon


    Thanks for that. Much appreciated. I think i get it now. Cheers.


Advertisement