0

How is the Output of this B? If I put this code in playground it just gives me errors.

class A { public void do() { System.out.println(''A''); } public void do(String str) { System.out.println(str); } } class B { public static void main(String[ ] args) { A object = new A(); object.do(''B''); } }

4th Mar 2018, 3:32 PM
Sebastian Sivertsen
Sebastian Sivertsen - avatar
4 ответов
+ 1
you have two methods do(). first is without any parameters, second is overloaded and has String parameter. when you call do() method in main(), you call it with String parameter “B”, that’s why you call second version of method do(), which print this parameter.
4th Mar 2018, 3:56 PM
solntsa
solntsa - avatar
+ 1
you have error because of the method name. ‘do’ is the reserved keyword in java. you should just change name of your method. for example, https://code.sololearn.com/cV1t35DKfB6v/?ref=app
4th Mar 2018, 3:51 PM
solntsa
solntsa - avatar
+ 1
Okay thank you, I was really wondering there because it‘s part of the SoloLearn Java course. But can you still explain to my why the output is B? :)
4th Mar 2018, 3:53 PM
Sebastian Sivertsen
Sebastian Sivertsen - avatar
0
i've read the code twice and i suppose it's because of the name of the method "do" try to give it a different name like "Do" just because i suppose the compiler reads that "do" as the instruction "do....while ()"
4th Mar 2018, 3:38 PM
Alessio Bocini
Alessio Bocini - avatar