V
V
Verg1l2015-02-19 16:10:51
Java
Verg1l, 2015-02-19 16:10:51

What are the ways to pass a variable from parent to child class?

I'm having a problem passing the value of a variable from parent to child class.
As I understood property values ​​are not inherited.
The constants don't fit.
With a DB and files of a configuration too not a variant.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Pukhov, 2015-02-20
@Verg1l

judging by the correspondence, you have class A and class B, which is the heir of A, that is, B: A
has 2 objects of class A and B,
you change something in the object from A and want to see something in the object from B, the
answer is no , you can’t do that. Either you need to build the architecture correctly, advice was in the answers of Viktor Gogilchin and Yuri Vitkovsky

V
Vladimir Abramov, 2015-02-19
@kivsiak

What is the difference between private and protected, do you understand?

V
Victor Gogilchin, 2015-02-19
@lslayer

Use the magic word super =)
Parent class:

public class Parent {
    
    private String valueForPass = "some value";
    
    protected final String getValueForPass() {
        return valueForPass;
    }

}

Child class:
public class Child extends Parent {
    
    private String myOtherValue = super.getValueForPass();
    
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question