Back to articles
#16 Known is a Drop! Tricky Coding question in Method Overloading JAVA

#16 Known is a Drop! Tricky Coding question in Method Overloading JAVA

via Dev.to BeginnersDeepikandas

Cheatsheet before seeing Tricky Questions Method Overloading Tricky Questions : 1. class Test { void show(int a) { } void show(long a) { } public static void main(String[] args) { Test t = new Test(); t.show(10); } } Answer: void show(int a) is called. 2. class Test { void show(Integer a) { } void show(int a) { } public static void main(String[] args) { Test t = new Test(); t.show(10); } } Answer: void show(int a) is called. 3. class Test { void show(int a, float b) { } void show(float a, int b) { } public static void main(String[] args) { Test t = new Test(); t.show(10, 10); } } Answer: compilation error:The method show(int, float) is ambiguous for the type Test No float type arguments passed 4. class Test { void show(int... a) { } void show(int a) { } public static void main(String[] args) { Test t = new Test(); t.show(10); } } Answer: void show(int a) is called. 5. class Test { void show(int a) { } void show(int... a) { } public static void main(String[] args) { Test t = new Test();

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
4 views

Related Articles