Originally posted by Richard Huang:
I cannot see why the answer should be A. Can somebody give me some help?
Given:
1. class Super {
2. public float getNum() { return 3.0f; }
3. }
4.
5. public class Sub extends Super {
6.
7. }
Which method, placed at line6, causes compilation to fail?
A. public void getNum() { }
B. public void getNum(double d) { }
C. public float getNum() { return 4.0f; }
D. public double getNum(float d) { return 4.0d; }
First, see that Sub extends Super. Since the method in super is public, Sub inherits that method. Now, if you want to
override that method you would do something like choice C. However, because this method is inherited, if you want to overload it, remember that changing the return type is insufficient to overload a method. The parameter list needs to be changed
atleast. Therefore, having known the 2 above things, it can't be C because that is a proper override, and it can't be B or D because those are proper overloads. What we have left is A because it is not a valid overload. Hope that helps! Good luck as you prepare for the exam!