1

there is something i don't get while trying to access a protected member. Usually we say protected member can be accessed from the same package or from subclasses by calling (int protected;) this way

System.out.println(protected);

But something strange occurs when trying to call the member by an object. Here is what i did and didn't get I have got 3 packages

1- javaapplication1 //the root package
    |- javaapplication1.entities (inside javaaplication1)
    |- javaapplication1.newpackage (inside javaapplication1 also)

And I have 3 classes Personne.java and Employe in entities package, and AlphaEmploye in newpackage

Personne.java:

package javaapplication1.entities;
import javaapplication1.newpackage.AlphaEmploye;
public class Personne {
    private int i;
    protected int j;
    public int k;
    public Personne(){
        i=8;
        j=9;
        k=10;
    }
    @Override
    public String toString() {
        return "Personne{" + "i=" + i + ", j=" + j + ", k=" + k + '}';
    }
    public void test(Personne p, Employe e, AlphaEmploye ae){
        System.out.println(" "+p.i+p.j+p.k);
        System.out.println(" "+e.i+e.j+e.k); //e.i doesn't work <-- explain this
        System.out.println(" "+ae.i+ae.j+ae.k); //ae.i doesn't work 
    }
}

Employe class :

package javaapplication1.entities;

import javaapplication1.newpackage.AlphaEmploye;


public class Employe extends Personne{

    public Employe(){
    }

    public void test(Personne p, Employe e, AlphaEmploye ae){
        System.out.println(" "+p.i+p.j+p.k); // p.i doesn't work
        System.out.println(" "+e.i+e.j+e.k); // e.i doesn't work <-- explain this
        System.out.println(" "+ae.i+ae.j+ae.k); //ae.i doesn't work <-- explain why ae.j works
    }
}

And AlphaEmploye

package javaapplication1.newpackage;

import javaapplication1.entities.Employe;
import javaapplication1.entities.Personne;

public class AlphaEmploye extends Personne{

    public AlphaEmploye(){
    }

    public void test(Personne p, Employe e, AlphaEmploye ae){
        System.out.println(" "+p.i+p.j+p.k); //p.i AND p.j doesn't work <-- explain this
        System.out.println(" "+e.i+e.j+e.k); // e.i AND e.j doesn't work <-- explain this
        System.out.println(" "+ae.i+ae.j+ae.k);// ae.i doesn't work <-- explain this
    }
}

I do know what protected modifiers do usually, but when trying to call protected members from an object i don't get what happens.

The one who closed the question pasted a question wich doesn't answer me. Here is what the link says /

Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

In addition, if Id denotes an instance field or instance method, then:

If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type

of the expression Q is S or a subclass of S.

If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

How this answers how the acces via objects works and why can i reach ae.j works from employe class. Please read before closing or even tell me why do you think it has already been answered. It only deals with one scenario without even explaining the logic behind it.

Jtx
  • 7
  • 2
  • I'm using netbeans and OpenJDK 8 – Jtx Apr 02 '18 at 00:19
  • What do you mean by "it doesn't work"? Compilation errors? Different output to what you expect? – Stephen C Apr 02 '18 at 00:20
  • "System.out.println(protected);" wont work because "protected " is a key word in java – Ayush Nigam Apr 02 '18 at 00:35
  • Access to private members - class only (they are *not* inherited by the subclasses of the class in which they are defined.). Access to protected members from subclasses in another package is via inheritance only, not via an object. – Zippy Apr 02 '18 at 00:44
  • *"I'm using netbeans and OpenJDK 8"* - That is irrelevant. If you answered my questions, someone might be able to help you. Otherwise, just read the dup link. – Stephen C Apr 02 '18 at 01:52
  • Stephen C : I added comment before you, so my comment couldn't reply to you. I meant compilation error, it says j is protected Ayush : just read what i did before Zippy : thanks but then what are rules via an object? – Jtx Apr 02 '18 at 07:52

0 Answers0