Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (i.e. 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 )

like image 799
Jayant Avatar asked Jul 04 '12 01:07

Jayant


People also ask

Can a parent class access child class variable?

The only unusual aspect is that, within child class method definitions, you can't directly access parent class instance variables. For example, if the parent had a height instance variable, child class method definitions wouldn't be able to access this directly.

Can a parent class call child class method?

Why we can't call child specific methods with the help of parent reference on child object in Java? Imagine that your child class is inheriting the parent class. Then the child class can define it's own methods, that are not defined in the parent class and not inherited by the child class.

Can a parent reference point to child object in Java?

The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.


1 Answers

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();
    }
}
like image 127
Victor Wong Avatar answered Oct 17 '22 15:10

Victor Wong