What is the result of compiling the following program?
package mypkg;/*from w w w. j av a 2s .c om*/ class MyClass {} class MyClass2 {} abstract public class Shape { public MyClass getPartner() { return new MyClass(); } abstract public MyClass getPartner(int count); // u1 } abstract class Rectangle extends Shape { public MyClass2 getPartner() { // u2 return new MyClass2(); // u3 } }
B.
The Shape class compiles without issue, making Option A incorrect.
The Rectangle class, though, does not compile because getPartner()
is an invalid method override.
In particular, MyClass and MyClass2 are not covariant since MyClass2 is not a subclass of MyClass.
Therefore, line u2 does not compile, making Option B correct and Option D incorrect.
Note that the abstract method getPartner(int)
is not implemented in Rectangle, but this is valid because Rectangle is an abstract class and is not required to implement all of the inherited abstract methods.