开发者

Java extends example

开发者 https://www.devze.com 2023-01-25 00:21 出处:网络
i have a java beginner question: Parent.print() prints \"hallo\" in the console, but also Child.print() prints \"hallo\".

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";

    pu开发者_Go百科blic void print() {
        System.out.println(output);
    }

}

public class Child extends Parent {

   private String output = "child";

}


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.


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();
}


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.


When I was trying to figure out the extend keyword, I was using two classes. I'll hope that also will help you to understand the basic idea.

Parent.java

public class Parent {
    private int a1;
    private int b1;


    public Parent(int a, int b){
        this.a1 = a;
        this.b1 = b;
    }

    public void print() {
        System.out.println("a1= " + this.a1 + " b1= " + this.b1);
    }

}

Child.java

public class Child extends Parent {
        public Child(int c1, int d1){
        super(c1,d1);
    }

    public static void main(String[] args) {
        Parent pa = new Parent(1,2);
        pa.print();
        Child ch = new Child(5,6);
        ch.print();
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消