+ 1

Override

I thought that methods that ovverride must have the same return type and arguments. So how it it that a method with no parameters be overwritten by a method with 1 parameter? What is the output of this code? class A { public void doSomething() { System.out.println("A"); } public void doSomething(String str) { System.out.println(str); } } class B { public static void main(String[ ] args) { A object = new A(); object.doSomething("B"); } } //Outputs "B"

29th Jul 2020, 4:53 PM
Ray
3 Respuestas
+ 3
You have overloaded methods. Not overwritten. Overloading means defining methods with same name with different arguments types or different number of arguments... Overriding means implementing method in sub class, with same name and same number of arguments and type which is in super class..
29th Jul 2020, 4:57 PM
Jayakrishna 🇮🇳
+ 1
B is the output.
30th Jun 2022, 7:32 PM
Opanuga Stephen
Opanuga Stephen - avatar
0
output B
7th May 2022, 4:37 PM
U.L.F. Feroza