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 }
5 Answers
+ 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
+ 2
and you need to use main() method for test it
+ 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 :)
+ 1
zemiak thank you for your tip as well