- 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''); } }
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.
- 1
wot
- 13
B