Java extends example

48,267

Solution 1

Currently you've got two separate variables, and the code in Parent only knows about Parent.output. You need to set the value of Parent.output to "child". For example:

public class Parent {

  private String output = "hallo";

  protected void setOutput(String output) {
    this.output = output;
  }

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

public class Child extends Parent {
  public Child() {
    setOutput("child");
  }
}

An alternative approach would be to give the Parent class a constructor which took the desired output:

public class Parent {
  private String output;

  public Parent(String output) {
    this.output = output;
  }

  public Parent() {
    this("hallo");
  }

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

public class Child extends Parent {
  public Child() {
    super("child");
  }
}

It really depends on what you want to do.

Solution 2

Child doesn't have access to Parent's output instance variable because it is private. What you need to do is make it protected and in the constructor of Child set output to "child".

In other words, the two output variables are different.

You could also do this if you change output to be protected in Parent:

public void print(){
    output = "child"
    super.print();
}

Solution 3

The reason why child is not printing "child" is that in inheritance in java, only methods are inherited, not fields. The variable output is not overridden by the child.

You could do it like this:

public class Parent {
    private String parentOutput = "hallo";

    String getOutput() {
        return output;
    }

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

public class Child extends Parent {
    private String childOutput = "child";

    String getOutput() {
        return output;
    }
}

Also, the String variables do not need to be different names, but I did so here for clarity.

Another, more readable way would be to do this:

public class Parent {
    protected String output;

    public Parent() {
        output = "hallo";
    }

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

public class Child extends Parent {
    public Child() {
        output = "child";
    }
}

In this example the variable is protected, meaning it can be read from both the parent and child. The constructor of the classes sets the variable to the desired value. This way you only implement the print function once, and do not need a duplicate overridden method.

Share:
48,267
Martin Kapfhammer
Author by

Martin Kapfhammer

Updated on July 11, 2022

Comments

  • Martin Kapfhammer
    Martin Kapfhammer almost 2 years

    i have a java beginner question: Parent.print() prints "hallo" in the console, but also Child.print() prints "hallo". I thought it has to print "child". How can i solve this?

    public class Parent {
    
        private String output = "hallo";
    
        public void print() {
            System.out.println(output);
        }
    
    }
    
    public class Child extends Parent {
    
       private String output = "child";
    
    }
    
  • Jon Skeet
    Jon Skeet over 13 years
    @Martin: Because you were creating a completely separate variable, and setting the value of that in Child. The Parent class is unaware of the variable in Child.