Send the subclass as an argument in the super constructor

1

If a class in your constructor calls super(/*argumentos*/); , how do I send it to itself? (Likewise as a parameter, what else could be sent? And I know that the subclass, the superclass can not call super(); (at least not in my example))

public abstract class Parent {
Otro objeto;
public Parent (String variableNoImportante) {
objeto = new Otro(variableNoImportante,VARIABLEOBJETOHIJO);}}

Here you have to send to the object that calls it, if it is an instance of Child or if it is any other subclass what it sends as a parameter is the object that invokes super(str); .

public class Child extends Parent {
public Child(String variableNoImportante) {
super(variableNoImportante);
}}
public class Otro {
public Otro(String varNoImp,Parent objChild) {
/*do something*/}}

The problem is that when in the third object I occupy objChild.variables , it gives me the default variables that I created for Parent instead of those overwritten by Child . Obvious, because I use this.

If I replace it with: super(variableNoImportante,this); and public Parent(String varNoImp,Child hijo) {objeto = new Otro(varNoImp,hijo);} then it tells me that I can not use this in a constructor.

Any suggestions? I do not even know why it returns the parameters of Parent if it is abstract. (I do not understand why they get so complicated, it does not matter what I want to achieve, this is a general example, because this is a community, if I put my project as such then I could not help someone else, only me) (The inheritance is precisely because I can thus occupy the same constructor for all classes.) I can not create another Child object because then for it to create it before, if I do it it would make a loop: the Child constructor calls the Parent constructor that creates a new Child. Also obviously there are more things in the Child constructor and more parameters, if I put a new Child (worth the redundancy) a new one is created without parameters and the other ones where they are? Also yes, Child is not the only subclass that Parent has. Is not there a way to prevent the constructor from being overwritten?

    
asked by Ica Polinesio GarLop 30.06.2017 в 21:44
source

0 answers