Java example extends

I have a Java beginner question: Parent.print () prints "hallo" in the console, but also Child.print () prints "hallo". I thought he should print "baby." 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"; } 
+7
java extends superclass
source share
4 answers

You currently have two separate variables, and the Parent code only knows about Parent.output . You must 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 that takes the desired result:

 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.

+7
source share

Child does not have access to the Parent output instance variable because it is private . You need to make protected and set the output to "child" in the Child constructor.

In other words, the two output variables are different.

You can also do this if you change the output to protected in Parent :

 public void print(){ output = "child" super.print(); } 
+3
source share

The reason the child does not print "child" is because in java inheritance only methods are inherited, not fields. The variable output not overridden by the child.

You can do it as follows:

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

In addition, String variables need not be different names, but I did it 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 protected , that is, it can be read from both the parent and the child. The class constructor sets the variable to the desired value. Thus, you only implement the print function once and do not need a duplicated overridden method.

0
source share

When I tried to figure out the extend keyword, I used two classes. I hope this also helps you 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
source share

All Articles