简体   繁体   中英

In Java how to refer subclass variable without declaring that variable in parent class?

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

class A {
    private final int x = 5;

    protected int getX() {
        return x;
    }

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

class B extends A {
    private final int x = 10;

    @Override
    protected int getX() {
        return x;
    }


}

In this example, I need to print subclass value in the parent class. It is working fine. No issue. Now it is printing 10. But I do not want to define that property in the parent class A .

Because in this example this x datatype is very simple. So no issue. But in real-time I want to use other datatype which may be another Class variable or List<something> which have huge data.

So ultimately I do not wish to store that value in Class A . Because it is redundant data. It will slow down in my Hibernate thing. Please let me know, how to achieve this without declaring variable in parent class. But I still need to use subclass variable in parent class.

make abstract your class A and the getX(); method.

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

abstract class A {

    protected abstract int getX();

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

class B extends A {
    private final int x = 10;

    @Override
    protected int getX() {
        return x;
    }
}

and override the toString method in place of your print method

@Override
public String toString() {
    return String.valueOf(getX());
}

the final code

public class Test {
    public static void main(final String[] args) {
        B b = new B();
        System.out.println(b);
    }
}

abstract class A {

    protected abstract int getX();

    @Override
    public String toString() {
        return String.valueOf(getX());
    }
}

class B extends A {
    private static final int X = 10;

    @Override
    protected int getX() {
        return X;
    }
}

you could also define as static your x variable

But as say Andrew Tobilko you can consider also to use an interface if A doesn't represent a stateful entity.

It's certainly the best solution for your case, mix the use of an interface and an abstract class

public class Test {
    public static void main(final String[] args) {
        B b = new B();
        System.out.println(b);
    }
}

interface MyInterface {
    int getX();
}

abstract class A implements MyInterface{
    @Override
    public String toString() {
        return String.valueOf(getX());
    }
}

class B extends A {
    private static final int X = 10;

    @Override
    public int getX() {
        return X;
    }
}

You need the getX within the parent class, but you don't have information enough to implement this method there.

You can declare this class as abstract and mark the method with abstract as well. Doing that, you are handing the responsibility of method implementation over its subclasses and preventing from parent field declaration.

If the A doesn't describe any state (only actions/methods), you should consider replacing it with an interface. At the current state, it is the case.

You could make the parent class abstract, eliminate the property in the parent class, make getX() abstract, and then leave print() as concrete. Then just use the concrete implementation of getX() in the child class.

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