In inheritance with private attributes, does not the daughter class take its attributes from the mother class?

Asked

Viewed 7,150 times

4

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

In inheritance with private attributes, the daughter class does not take its attributes from the mother class?

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 daughter class. This class even welcomes the private members of its mother, but cannot access them in the code of the child class, nor can it access the members of the mother through super. When opting for a private member is saying that he can not leak "under any circumstances" from his class, not even the derivatives.

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

Members that must be accessed by child classes must be declared as protected. There is an inheritance of these members with visibility in it, as well as the public, obviously. I think it is also obvious that the protected cannot be accessed outside the daughter class.

Another common mistake is to think that the child class accesses members of another class when there is inheritance, this is not the case. The mother class members become part of the daughter class, not accessing elsewhere. There are no two distinct objects, one for the mother instance and the other for the daughter instance, there will only be the daughter instance that holds all the members, their own and the mother’s.

Well, roughly speaking, it’s like the compiler copied everything from the mother class and glued it to the daughter class. You’re not seeing the members there in your code, but they’re there when you put that down to inherit from a class. Private members are copied, but not visible to the child class code.

The methods accessors are generally public and are inherited. If you do not want them to be public but want the daughter class to have access to them, then they should be protected, there’s no other way. And of course the variable that will support these methods must have at least the same visibility - I would say at most the same too, it may not be so, but it doesn’t make any sense.

  • I had this doubt because it is often put as default values for attributes with initialization in constructors. I thought that if I inherit a class that has these initializations in the constructor and I give super in the daughter class I could not get those initialized values from the mother class?

  • So if they go public or protected, they will be in the daughter class and can access them like this. If they are private, there is no way to access them directly by the daughter class, can through a method super, I’m not sure I understand that question. I’m not sure I have. When so try to put one in the question a concrete example of what you are trying to do, so it is easier to visualize and propose an alternative.

  • OMG! Now I understand the builder is public.

  • In general, yes, it is possible to do it privately, but in a few cases this is useful.

3

Private attributes are only accessible in the class that implements them. Child classes do not have access. If you want an attribute with no public visibility, but the child classes can access, use the property protected

Edit: I’m not allowed to comment so I’m going to add an addendum regarding the use of super. It allows you to access the implementation of a parent class method. It cannot 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
}
  • But in the parent class constructor if you have attributes already initialized I can super in the daughter class to get the initialization of attributes, no?

  • Shouldn’t it be? public ABCD2(){ super(); }

  • No, super only serves to invoke the parent class implementation of a method. You can’t super use it to invoke the parent class structure. What is private will not be visible. Everything else, protected and public, is visible.

  • Just programming for me to see this. I’m going to do this simple test.

  • Understand that super just summons, does not give you access. If private attributes are being changed in a method you can call the super, the values will actually be changed, but even so it will not give you direct access to the attribute.

  • Okay. Thank you. :)

Show 1 more comment

1

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

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

  • But what about the question of inheritance? I can’t inherit the attributes and have access to them using the super?

  • 2

    Classes in the same package can access protecteds even if they are not heirs, no?

  • They can, a class in the same package can instantiate/take an instance of the other class in the same package and read its protected attributes

  • ops. I really mistook the default

0

The daughter class takes the attributes of the mother class: yes and no.

Yes

They exist in the daughter class, they just can’t be accessed by her, to solve this you can:

  • put the access modifier of the variable/method as protected or public
  • create a method get or set as getNome public or protected, for example to take or set the value in the mother class variable
  • Using Reflection, there is a feature that you "ignore" all java access paradigms and simply access/change the data of any object

Not

If you don’t use any of the previous tricks it’s really impossible

Edited

There are several ways to implement what I said so go just one example

Example of how to set in 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");
      }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.