0

(JAVA) How to complete this code?

What should I write instead of ??? to get a correct result (output) and why: This is the code: 0 class ExampleB { 1 2 float x = 7f; 3 4 } 5 6 class ExampleA extends ExampleB { 7 8 float x = 5f; 9 10 public void method () { 11 float x = 2f; 12 System .out. println ("Die Variable aus Zeile 2 hat den Wert : " + ???); //here 13 System .out. println ("Die Variable aus Zeile 8 hat den Wert : " + ???); //and here 14 } 15 16 }

10th May 2020, 6:57 PM
Mohammad Ala Tahhan
Mohammad Ala Tahhan - avatar
5 Respuestas
+ 4
Hello Mohammad Ala Tahhan You need to learn about the keywords this and super: https://www.baeldung.com/java-this https://www.baeldung.com/java-super public void method() is inside ExampleB, which is the child class of ExampleA. If you want to print x from Line 2 you need to use super because you mean x from your super class: super.x If you want to print x from Line 8 you have to use this because.you mean x from your current class: this.x
10th May 2020, 7:30 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
and you need to use main() method for test it
10th May 2020, 10:59 PM
zemiak
+ 1
Denise Roßberg thank you so much for your explanation and the links. That was really helpful and exactly what I was looking for. I hope there was more than an UpVote :)
13th May 2020, 2:12 AM
Mohammad Ala Tahhan
Mohammad Ala Tahhan - avatar
+ 1
zemiak thank you for your tip as well
13th May 2020, 2:13 AM
Mohammad Ala Tahhan
Mohammad Ala Tahhan - avatar