-3

How would i change the value of a value in the parent class using the child class? For example the parent class holds a int value of 4 and I want the child class to change that value to 8.

fghrmtukrt
  • 11
  • 1
  • 2
    If that field is not private, you can simply change it. If it is private, you cannot change it (except if there's a setter or you use ugly reflection) – f1sh Feb 21 '19 at 13:01
  • 1
    By using it's setter. – Sebastian Feb 21 '19 at 13:01
  • 3
    Possible duplicate of [Java Inheritance - instance variables overriding](https://stackoverflow.com/questions/2464222/java-inheritance-instance-variables-overriding) Well explained – xxxvodnikxxx Feb 21 '19 at 13:03
  • 3
    Please show the code you have tried and what problems you encountered. – Adriaan Koster Feb 21 '19 at 13:06

2 Answers2

1

The field could have been initialized as private, protected or even be a static field, these could all affect the editablity.

If you have initialized it as private in the parent class with a setter, you can create an instance of the parent class in the child class. If you set a new value in that instance created in the child class, it changes value everywhere.

See this parent class;

public class Parent{
    private int girl = 4;

    public Parent(){}

    public int getGirl() {
        return girl;
    }

    public void setGirl(int girl) {
        this.girl = girl;
    }
}

And this Child class;

public class ChildClass{
    Parent parent = new Parent();
    parent.setGirl(8);

}
J.Kwere
  • 11
  • 3
  • 1
    I am sorry, but that doesn't make much sense. These classes are called "Parent" and "ChildClass", but what goes on with the fields can be done with any classes, even without inheritance. You're just calling a setter - it has nothing to do with parent-child-relationship. I think a child class creating an instance of it's parent class is not what OP wants. – bkis Feb 21 '19 at 13:14
  • 1
    Well, he didn't explain what he wanted clearly or use a code snippet or anything like. In plain terms, I thought that was what he wanted. But If it isn't, my bad then. – J.Kwere Feb 21 '19 at 13:19
  • 1
    You are right, the question is very vague. I just thought if it's clearly about "parent" and "child" classes, the solution should be based on inheritance. But hey, don't get me wrong, i only wanted to point that out. Maybe it *is* what they were looking for. – bkis Feb 21 '19 at 13:23
1

First, just to clarify (although it might be nitpicking): You don't change the value of a variable of a class. You change the value referenced by a variable of an instance of a class.

If this class (say C) extends another class (say P), than P might have a protected field v you can just change in C (because C has access to the parent's v).

public class Parent {
    protected int var = 8;
}

public class Child extends Parent {
    public void samething() {
        var = 4;
    }
}

Even more possibilities: If that value should be the same in all instances of P (and thus also C), you might use static fields.

bkis
  • 2,530
  • 1
  • 17
  • 31