overloading why output B ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 6

overloading why output B ?

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 Sep 2016, 2:30 PM
mahmoud elkeiy
mahmoud elkeiy - avatar
3 Answers
+ 3
This is how it works: You have 2 same methods. One is void method, without any parameters to take and other is void method with parameters. When you call method from class A (object.do("B"); You use method which requires some value in parameter and then prints the value, you have entered. If you would write object.do(); without giving any value in parameters, program would use the method, which doesn't require any parameters. Hope it's all clear and I helped you.
4th Sep 2016, 9:31 PM
Benas
- 1
wot
14th Sep 2018, 1:25 PM
Christian Noble
- 13
B
18th Aug 2017, 1:12 PM
PRIYADHARSHINI M