164k views
5 votes
Look at the following code. The method in line ________ will override the method in line ________. Line 1 public class ClassA Line 2 { Line 3 public ClassA() {} Line 4 public int method1(int a){} Line 5 public double method2(int b){} Line 6 } Line 7 public ClassB extends ClassA Line 8 { Line 9 public ClassB(){} Line 10 public int method1(int b, int c){} Line 11 public double method2(double c){} Line 12 }

1 Answer

3 votes

Answer:

The method in line 11 will override the method in line 5.

Step-by-step explanation:

As ClassB implements ClassA's functions, any functions with the same signature (name + arguments) will be overridden due to inheritance in Java.

There is a false herring as you could answer line 10 overrides the method as defined in line 4 - however this is not overridden as they do not share the exact same parameters (different parameter names are fine, however different parameter types are not), (int b, int c) as opposed to (int a).

Hope this helps, feel free to message me for any more information!

Edit 1:

I've just realised that method2 in ClassA takes an integer parameter, whereas method2 in ClassB takes a double as a parameter. This may be a typo as they return the same type from the function - however if it is not the case that this is a typo then I don't see any way how ClassB could override methods from ClassA.

User Firnagzen
by
3.5k points