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.