3

i have a few sub classes with unique static vars in this case called 'x'. All of those sub classes are using the static var the same way, so I want to reduce code duplication and put the functionality in the super class. In this case the method 'getX' in the super class. From here I want to return the value of x. Right now I'm facing the issue that it uses the x value of the super class and not the value of the child class. How can I access the x value of the sub class from the super class?

public class Playground {

  public static void main(String[] args) {
    Parent parent = new Parent();
    Child child = new Child();
    Child1 child1 = new Child1();

    System.out.println("Parent.x " + parent.x);
    System.out.println("child.x " + child.x);
    System.out.println("child.x " + child1.x);

    System.out.println("get x: " + parent.getX());
    System.out.println("get x: " + child.getX());
  }
}

class Parent {
  static String x = "static of parent";
  String y = "instance of parent";

  String getX() {
      return x;
  }
}

class Child extends Parent {
  static String x = "static of child";
  String y = "instance of child";
}

class Child1 extends Parent {
  static String x = "static of child1";
  String y = "instance of child";
}

This code prints out: Parent.x static of parent child.x static of child child.x static of child1 get x: static of parent get x: static of parent <-- here should be static of child

Hope someone can help me.

Cheers

DΦC__WTF
  • 105
  • 1
  • 9

5 Answers5

2

Try adding the getX method to the child. Like this:

class Child extends Parent {
  static String x = "static of child";
  String y = "instance of child";
  String getX() {
      return x;
  }
}
  • 2
    In my real scenario I have 6 sub classes, so I don't want to copy getX which are 3-4 methods (in the real scenario) in all sub classes – DΦC__WTF Jan 06 '17 at 12:29
  • But when you call the getX method, java calls the one from Parent. That's why you're getting the x from parent. I think this is the only way to get the expected result. – Rosário Pereira Fernandes Jan 06 '17 at 12:32
2

A solution could be to not have a static in the Parent, but an instance field.

class Parent{
    private final X x
    public Parent(X x){
        this.x = x;
    }

    public X getX() {
        return x;
    }
}

Then in your child, you still can have a static, but you pass it at the constructor

class Child extends Parent {
    static final X x = ....
    public Child() {
        super(x);
    }
}
seneque
  • 279
  • 1
  • 5
1

Add the getX method in the child class. The parent getX method points to the static variable of the parent class.

MSD
  • 1,409
  • 12
  • 25
1

What you're trying is a violation of the basic OOP principle information hiding / encapulation.

No other class should know about how a class stores or treats its properties (aka variables or fields). This includes that clases should not have getter and setter (as suggested by @RosárioPereiraFernandes)

There is an exception to this rule when the class represents a data transfer object (DTO) which has (almost) no business logic and just provides access to structured data. DTOs shoould have getters and (less often) setters.

Other classes should provide methods with busines related names to modify their inner state (the values of their member variables):

class Car{
  private final int maxSpeedInMph = 100;
  private int speedInMph = 0;
  public void accellerateBy(int speedDiffInMph){
    if( maxSpeedInMph >=speedInMph + speedDiffInMph )
       speedInMph += speedDiffInMph;
    else 
       speedInMph = maxSpeedInMph ;
  }    
  public void accellerateTo(int newSpeedInMph){
    if( maxSpeedInMph >=  newSpeedInMph)
       speedInMph = newSpeedInMph;
    else 
       speedInMph = maxSpeedInMph ;
  }
  public void decellerateBy(int speedDiffInMph){
    if( 0<=speedInMph - speedDiffInMph )
       speedInMph += speedDiffInMph;
    else 
       speedInMph = 0;
  }
  public void emengencyBreak(){
    speedInMph = 0;
  }
  // you get the idea?
}
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
  • I get that, I my case each child has a unique constant which I want to use. There is no manipulation. Think about a Bank with different types of BankAccounts and each type of BankAccount has a constant BankFee. So I want to have a method that returns the BankFee for each BankAccount without going into each child BankAccount and add a getter which returns the types BankFee. – DΦC__WTF Jan 06 '17 at 12:48
  • @DΦC__WTF : *" I my case each child has a unique constant which I want to use"* Why? - Most likely you plan to use it in `swich` statements. But you should rather use *plymorphism* to do specific actions based on a classes type. – Timothy Truckle Jan 06 '17 at 12:55
1

As far as I know it is not possible to access child-variables from the parent-class.

But you can override String getX() in the child class

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

Then Java calls the "lowest" getX()method


I would code your example different:

public class Playground {

private static Parent parent = new Parent("static of parent");
private static Child child = new Child("static of child");
private static Child1 child1 = new Child1("static of child1");

  public class Playground {

private static Parent parent = new Parent("static of parent");
private static Child child = new Child("static of child");
private static Child child1 = new Child("static of child1");

  public static void main(String[] args) {


    System.out.println("Parent.x " + parent.getX());
    System.out.println("child.x " + child.getX());
    System.out.println("child.x " + child1.getX());

    System.out.println("get x: " + parent.getX());
    System.out.println("get x: " + child.getX());
  }
}

 class Parent {
     private String x;
     public Parent(String x) {
         this.x = x;

     }

     public String getX() {
          return this.x;
      }
 }

class Child extends Parent {
    protected Child(String x) {
        super(x);
    }  
}

-> this will bring the following output:

Parent.x static of parent
child.x static of child
child.x static of child1
get x: static of parent
get x: static of child

Just ask if you have questions

KilledByCheese
  • 852
  • 10
  • 30