简体   繁体   中英

Java : Using parent class method to access child class variable

I have the following scenario :

public class A {

    private int x = 5;

    public void print()
    {
        System.out.println(x);
    }
}


public class B extends A {

    private int x = 10;

    /*public void print()
    {
        System.out.println(x);      
    }*/

    public static void main(String[] args) {
        B b = new B();
        b.print();
    }

}

On executing the code, the output is : 5.

How to access the child class(B's) variable(x) via the parent class method?

Could this be done without overriding the print() method (ie uncommenting it in B)?

[This is important because on overriding we will have to rewrite the whole code for the print() method again]

EDITED

More Clarification :-

  • The motive of the question is to use the value of a child class private variable from its parent class method. This doesn't require changing the value of the parent class private variable in order to achieve the desired result.
  • The answers posted here, though, led me to my desired answer, which I have posted below.

(Thanks all for your time and help )

class A {
    private int x = 5;

    protected int getX() {
        return x;
    }

    protected void setX(int x) {
        this.x = x;
    }

    public void print() {
        // getX() is used such that 
        // subclass overriding getX() can be reflected in print();
        System.out.println(getX());
    }
}

class B extends A {
    public B() {
        // setX(10);  // perhaps set the X to 10 in constructor or in main
    }

    public static void main(String[] args) {
        B b = new B();
        b.setX(10);
        b.print();
    }
}

EDITED

Below is a general answer using abstract class and method to solve similar scenario:

abstract class SuperA {
    protected abstract Object getObj();

    public void print() {
        System.out.println(getObj());
    }
}

class A extends SuperA {
    @Override
    protected Object getObj() {
        // Your implementation
        return null; // return what you want
    }
}

class B extends A {
    @Override
    protected Object getObj() {
        // Your implementation
        return null; // return what you want
    }

    public static void main(String[] args) {
        B b = new B();
        b.print();
    }
}

After reading all the answers posted here, I got what I was looking for. The following is what I feel is the best answer for my question :

public class A {
    private int x = 5;    
    protected int getX(){
        return x; 
    }    
    public void print(){
        System.out.println(getX());
    }
}
public class B extends A {
    private int x = 10;
    protected int getX(){
        return x; 
    }  
    public static void main(String[] args) {
        B b = new B();
        b.print();
    }
}

Setting up a protected getter and overriding it is better than overriding the print() method itself, as there could be any other huge method in place of the print method which might need to access the value of the child class variable(s).

To solve your question you have to define the fields in the parent class A like protected (so it will be inherited on the child class) and set the field value x inside the constructor in the child class B. The print method is also inherited from A class so you can invoke it directly from parent class.

I hope this can help you.

public class A 
{
    // fields declaration 
    protected int x = 5;

    public void print()
    {
        System.out.println(x);
    }
}



public class B extends A 
{

    public B()
    {
        // set child x value. The field have been defined in the parent class
        x = 10;
    }

    public static void main(String[] args) 
    {
        A a = new A();
        a.print(); // print 5

        B b = new B();
        b.print(); // print 10
    }

}

You should expose a getter for the value you want and override that in the child class.

Like so:

public class A {

    private int x = 5;

    public void print()
    {
        System.out.println(getX());
    }

    protected void setX(int x)
    {
        this.x = x;
    }

    protected int getX()
    {
        return x;
    }

}


public class B extends A {

    /*public void print()
    {
        System.out.println(x);      
    }*/

    public B()
    {
        setX(10);
    }

    public static void main(String[] args) {
        B b = new B();
        b.print();
    }

}

You can always add it to the constructor:

public class B extends A {

    //this line is unnecessary: private int x = 10;

    /*public void print()
    {
        System.out.println(x);      
    }*/

    public B()
    {
        x=10;
    }


    public static void main(String[] args) {
        B b = new B();
        b.print();
    }

}

The reason it won't work as you try it is that default values only get evaluated once. So when it's default 5 in A, it stays 5 even though you used default 10 in B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM