In inheritance with private attributes, does not the child class take its attributes from the parent class?

4

The devmedia article says that the private modifier does not give access to their attributes in their daughter classes. It has a table with the modifiers.

In inheritance with private attributes, does not the child class take its attributes from the parent class?

    
asked by anonymous 15.01.2016 / 19:10

4 answers

4

Private members are always restricted to the class in which they were declared. Even when there is inheritance, they are not accessible to the child class. This class does not receive the private members of its mother, nor is it able to access the members of the mother through super . When you opt for a private member, you are saying that it can not leak "under any circumstances" from your class, not even derivatives.

It is not so in any way so, it is possible to do via reflection, but it is a trick to go over the language. This can be abused. So it is not easy to do. It is necessary to understand that the protection that the language gives is only to cover the well-intentioned misuse. It is not a guarantee that there will never be access to members.

Members that are to be accessed by child classes should be declared as protected . There is inheritance from these members, as does public , obviously. I think it's obvious also that the protected ones can not be accessed outside the child class.

Another common mistake is to think that the child class accesses the members of another class when there is inheritance. This is not the case. Members of the parent class become part of the child class. You are not accessing elsewhere. There are no two distinct objects, one for the parent instance and one for the daughter instance. There will only be the daughter instance that has all the members, her own and the mother.

It's pretty much as if the compiler copied everything from the parent class and pasted into the child class. You are not seeing the members there in your code, but they are there when you put that is to inherit from a class. Private members are copied, but are not visible to the child class code.

accessor methods are generally public and are inherited. If you do not want them to be public but want the child class to have access to them then they should be protected . There is no other way. Of course the variable that will support these methods should have at least the same visibility - I would say at the very least the same too, it may not be so, but it makes no sense at all.     

15.01.2016 / 19:27
3

Private attributes are only accessible in the class that implements them. Daughter classes do not have access. If you want an attribute that is not publicly visible, but which child classes can access, use the protected

Edit: I'm not allowed to comment so I'm going to add an addendum about using super . It allows access to the implementation of a method of the parent class. Can not interact with attributes. Example:

public class ABC{
  public DEF()
  //implementação imaginária aqui
}
public class ABC2 extends ABC{
  @Override //não é necessário
  public DEF(){
    super(); //invoca a implementação da classe pai deste método.
             //Só pode ser usado na primeira linha do método caso não
             //queira dar uma sobrecarga/overload do método
             //Se o método fosse privado isso não seria possível
}
    
15.01.2016 / 19:14
1

When a variable or method is marked as private . Only the class you implemented directly will have access. Neither child classes can access private fields.

If you want this, use protected . It is private to other classes, but child classes can access.

    
15.01.2016 / 19:13
0

The child class takes the attributes of the parent class: yes and no.

Yes

They exist in the child class, just can not be accessed by it, to solve this you can:

  • put the access modifier of the variable / method as protected or public
  • Create a method get or set as% with public or protected , for example to get or set the value in the variable of the parent
  • Using reflection , there is a feature that ignores all java access paradigms and simply accesses / changes the data of any object

No

If you do not use any of the previous devices really is impossible

Edited

There are several ways to implement what I said so there's only one example

Example of how to set the private variables of the parent class

public class Mae {

      private String nome;

      protected String getNome() {
          return this.nome;
      }

      protected void setNome(final String nome) {
          this.nome = nome;
      }
}

public class Filha extends Mae {

      public void main(String args[]){
          new Filha().setNome("hahahahha setei o nome na minha mãe");
      }
}
    
20.01.2016 / 17:07