+ 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"
3 odpowiedzi
+ 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..
+ 1
B is the output.
0
output B