Which statement(s) are correct about the following code? (Choose all that apply)
public class Rectangle { protected static Integer chew() throws Exception { System.out.println("Rectangle is printing"); return 1; /*from w ww . java 2 s.co m*/ } } public class Square extends Rectangle { public Number chew() throws RuntimeException { System.out.println("Square is printing on wood"); return 2; } }
C, E.
The code doesn't compile, so option A is incorrect.
Option B is also not correct because the rules for overriding a method allow a subclass to define a method with an exception that is a subclass of the exception in the parent method.
Option C is correct because the return types are not covariant; in particular, Number is not a subclass of Integer.
Option D is incorrect because the subclass defines a method that is more accessible than the method in the parent class, which is allowed.
Finally, option E is correct because the method is declared as static in the parent class and not so in the child class.
For non-private methods in the parent class, both methods must use static (hide) or neither should use static (override).