-2

edit : it is not a duplicata : I already know by heart private is accessible within the class only, protected within class and subclasses, and other class within the same packages and public is accessible everywhere, no modifiers = package only. that is not what I am talking about. I am talking about accessing from an object : object.var. Just go read the exemple and try yourself to understand the compiler errors, you can't with just this simple rule.

Before closing the question, make sure you got it, it's not an easy question and if you think so you may have not get the question.

Here is my code and i don't get how object.privateVar and object.protectedVar behaves when calling it from different classes In a daughter A class i can call daughterBObject.protected while in java doc it says we can call object.protectedVar only if the class we are calling in is involved in creating the object.

heritage.closefamilly.Mother:

package heritage.closefamilly;

import heritage.faraway.Auntie;

public class Mother {
    private int priv;
    protected int prot;
    public int pub;

    public Mother(){
        priv = 1;
        prot = 5;
        pub = 10;
    }

    public void testInheritance(Mother m, Daughter d, Auntie a){
        System.out.println(""+m.priv+m.prot+m.pub);
        System.out.println(""+d.priv+d.prot+d.pub); // d.priv compiler error
        System.out.println(""+a.priv+a.prot+a.pub);// a.priv compiler error
    }
}

heritage.closefamilly.Daughter :

package heritage.closefamilly;

import heritage.faraway.Auntie;

public class Daughter extends Mother{

    public void testInheritance(Mother m, Daughter d, Auntie a){
        System.out.println(""+m.priv+m.prot+m.pub); // m.priv compiler error
        System.out.println(""+d.priv+d.prot+d.pub); // d.priv compiler error why ? we are in daughter class !
        System.out.println(""+a.priv+a.prot+a.pub); // a.priv compiler error, why does a.prot compile while we are in daughter class, it doesn't extends auntie neither it is auntie's subclass .. Javadoc says Object.prot shouldn't work when class it is called in is not involed in creating Object
    }
}

heritage.faraway.Auntie :

package heritage.faraway;

import heritage.closefamilly.Daughter;
import heritage.closefamilly.Mother;

public class Auntie extends Mother{

    public void testInheritance(Mother m, Daughter d, Auntie a){
        System.out.println(""+m.priv+m.prot+m.pub);// m.priv & m.prot compiler error
        System.out.println(""+d.priv+d.prot+d.pub); // d.priv & d.prot compiler error javadoc says " it is not involved in the implementation of mother and daughter"
        System.out.println(""+a.priv+a.prot+a.pub); // a.priv compiler error whY? we are in auntie class
    }
}

Can anyone explain these behaves to me?

Jtx
  • 7
  • 2
  • `d.priv` - `priv` variable is **not** in Daughter class. It is in the Mother class – Thiyagu Apr 02 '18 at 13:03
  • 2
    Possible duplicate of [In Java, difference between package private, public, protected, and private](https://stackoverflow.com/questions/215497/in-java-difference-between-package-private-public-protected-and-private) – Raman Shrivastava Apr 02 '18 at 13:12
  • 1
    https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html learn this – xMilos Apr 02 '18 at 13:25
  • Regarding your edit: your examples behave exactly how one would expect them to behave. I am not sure which part you don't understand. For example, Mother has a private variable "priv". Daughter can't access it because it is private, so accessing d.priv will never work. Is this the part you have trouble understanding? – Modus Tollens Apr 02 '18 at 14:51
  • for exemple : a.prot inside Daughter class works ! But daughter doesn't extends Auntie, neither auntie extends daughter. And daughter and auntie are not in the same package... How do you explain that? – Jtx Apr 02 '18 at 14:54
  • actually you'll find all question in quotes... I really tried to make all code easy to understand and questions easy to see , i strugled a lot to be clear and cannot do more than that exept if stackoverflow allow me to choose red police for some text – Jtx Apr 02 '18 at 15:10
  • my bad by "quotes" i meant in comments inside the code – Jtx Apr 02 '18 at 15:20

1 Answers1

0

First of all, a subclass does not inherit the private members of its parent class, meaning that the Daughter class doesn't actually contain priv. In order for this to work, you need to specify a public/protected method conventionally called (in this use case) getPriv() in the Mother class. In that method you return priv so that the subclasses can use the variable accordingly.

Secondly, there is no point of implementing the method testInheritance in the child classes, because it is basically the same as in the Mother class. If you were to change this method in the child classes, so that they provide a different implementation based on their functionality, then it would make sense. In such a case, you would need to use the annotation @Override.

I think this would be a good read for you to understand the whole concept of Inheritance theoretically : https://beginnersbook.com/2013/03/oops-in-java-encapsulation-inheritance-polymorphism-abstraction/

nasdenkov
  • 54
  • 10
  • it is exactly what i already know. My bad for forgetting @override but it didn't change any single thing. The compiler still does the same errors. If I put a getter for priv, it means priv IS actually in the subclasses and these subclasses DOES contains priv. But let's assume what you said is totally exact, then why does m.priv only compile within Mother class? And about protected? just try to think about it you won't get how it behaves. The point behind writting in all classes the same testInheritance is so that we can see from the IDE where the compiler won't compile. Just try it – Jtx Apr 02 '18 at 14:39
  • a.prot inside Daughter class works ! But daughter doesn't extends Auntie, neither auntie extends daughter. And daughter and auntie are not in the same package. None of the links posted above wich assumed i didn't know the java basics deals with that problem.. Let's assume that question is not that easy and is a real question – Jtx Apr 02 '18 at 14:43
  • If you want to call m.priv in the subclasses, you need to use super. That's the only way you can access variables from an upper class. The problem here is the identifier which you have specified as private, meaning that the child classes will not have priv unless you use the getter method. And EXACTLY the getter method allows the subclasses to have the priv variable contained inside of them, because a getter is public. a.prot inside the Daughter class works because auntie and daughter are both children of the mother, so they inherit the exact same value. – nasdenkov Apr 03 '18 at 19:55
  • Packages here are not relevant at all, because you never use the default identifier ( basically just giving it the datatype without an identifier ) . – nasdenkov Apr 03 '18 at 19:56