This is some information on the Java 6 certification that I prepare. This information is not very well structured and it is most of the time an answer to the questions that come when I read the SCJP 6 book (Bates and Sierra). *** AS OF 5 OF MARCH 2010, I AM SCJP 6.0 ***

Friday, January 29, 2010

26 - Review - protected - level basic - Java 1.6.0_17 - NB 6.8

??? protected ??? All the buzz about this keyword...

Let's try to do it simple...

http://en.wikibooks.org/wiki/Java_Programming/Keywords/protected

protected means default + inheritance

  • If you are in a separate pkg, you have access to the protected members ONLY if you extend the class where the protected member is defined.
  •  In the class that extends the class where the protected member is defined you have access to the member using DIRECTLY the member name (only if you didn't you the same name in the class and in this case the member protected is hidden) and using the keyword 'super'. 
  • If the protected member is hidden then use super. If you want to access a method protected in a subclass user the member name (or again if it is hidden then use super).
  • Don't try to user super.super (or super.super.super). It is not implemented... If you want a member to be accessed from all the inheritance tree then make the name unique or (it is a possibility) make a kind of proxy member that will have to call super from the parent class (I don't like this).
example :






/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg3.pkg31;

/**
 *
 * @author Rudy
 */
public class Class1 {

    private int a;
    private int b;

    protected int c = -1;

    protected int d = -888;

    protected Class1(int a, int b) {
        System.out.println("Class1-protected Class1(int a, int b)");
        this.a = a;
        this.b = b;
    }

    protected int calculation() {
        System.out.println("Class1-protected int calculation()");
        return a + b;
    }


    protected void simplyToAnnoyYou() {
        System.out.println("simplyToAnnoyYou");
    }




    public static void main(String[] args) {
        System.out.println("main from pkg31.Class1\n");

        System.out.println(new Class1(1, 2).calculation());
    }
}

run:
main from pkg31.Class1

Class1-protected Class1(int a, int b)
Class1-protected int calculation()
3
BUILD SUCCESSFUL (total time: 0 seconds)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg3.pkg32;

/**
 *
 * @author Rudy
 */
public class Class2 extends pkg3.pkg31.Class1 {

    Class2(int a, int b) {
        super(a, b);
        System.out.println("Class2(int a, int b)");
        super.c = -2;
    }

    @Override
    protected int calculation() {
        System.out.println("Class2-protected int calculation()");
        return super.calculation();
    }

    protected int calculation1() {
        System.out.println("Class2-protected int calculation1()");
        int a = super.calculation() + super.c + c;
        return a;
    }

    public static void main(String[] args) {
        System.out.println("main from pkg32.Class2\n");

        Class2 class2 = new Class2(1, 3);

        System.out.println("class2.calculation :  " + class2.calculation());

        System.out.println("c from class1 (protected) in class 2: " + class2.c);

        System.out.println("calculation1 : " + class2.calculation1());
    }
}

run:
main from pkg32.Class2

Class1-protected Class1(int a, int b)
Class2(int a, int b)
Class2-protected int calculation()
Class1-protected int calculation()
class2.calculation :  4
c from class1 (protected) in class 2: -2
Class2-protected int calculation1()
Class1-protected int calculation()
calculation1 : 0
BUILD SUCCESSFUL (total time: 0 seconds)






/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg3.pkg32;

/**
 *
 * @author Rudy
 */
public class Class3 extends Class2 {

    int d = -999;

    Class3(int a, int b) {
        super(a, b);
        System.out.println("Class3(int a, int b)");
        super.c = -4;
    }

    @Override
    protected int calculation() {
        System.out.println("Class3.calculation()");
        return super.calculation();
    }

    @Override
    protected int calculation1() {
        System.out.println("Class3.calculation1()");
        int a = super.calculation() + super.c + c;
        return a;
    }

    int getD() {
        return super.d;
    }

    public static void main(String[] args) {
        System.out.println("main from pkg32.Class2\n");

        Class3 class3 = new Class3(1, 3);

        System.out.println("class3.calculation :  " + class3.calculation());

        System.out.println("c from class1 (protected) in class 2: " + class3.c);

        System.out.println("calculation1 : " + class3.calculation1());

        System.out.println("d : " + class3.d);
        
        System.out.println("d from class1 and not defined in class 2 but hidden in class3 : " + class3.getD());

        class3.simplyToAnnoyYou();
    }
}

run:
main from pkg32.Class2

Class1-protected Class1(int a, int b)
Class2(int a, int b)
Class3(int a, int b)
Class3.calculation()
Class2-protected int calculation()
Class1-protected int calculation()
class3.calculation :  4
c from class1 (protected) in class 2: -4
Class3.calculation1()
Class2-protected int calculation()
Class1-protected int calculation()
calculation1 : -4
d : -999
d from class1 and not defined in class 2 but hidden in class3 : -888
simplyToAnnoyYou
BUILD SUCCESSFUL (total time: 0 seconds)

Can you explain all the output ?
:-)



No comments:

Post a Comment

Followers