Which code fragments will compile? (Choose all that apply.)
A. interface Base2 implements Base { }
B. abstract class Class2 extends Base { public boolean m1() { return true; } }
C. abstract class Class2 implements Base { }
D. abstract class Class2 implements Base {public boolean m1() { return (true); } }
E. class Class2 implements Base { boolean m1() { return false; } byte m2(short s) { return 42; } }
This message was edited 1 time. Last update was at by Bear Bibeault
A. "X extends Y" is correct if and only if X is a class and Y is an interface.
B. "X extends Y" is correct if and only if X is an interface and Y is a class.
C. "X extends Y" is correct if X and Y both are classes or both are interfaces.
D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.
This message was edited 2 times. Last update was at by shoeb sattar
Edited to remove direct answer. We try to provide direction, and try to help people learn how to learn--not just give out answers. Please see DoYourOwnHomework.
This message was edited 1 time. Last update was at by David Newton
As people will see from the "Beginning Java" title page, simply giving out answers helps nobody, so thank you David for deleting that post.
Please tell us what you think the correct answer is, and let us see what we think about it.
To avoid any more direct answers, I shall close this thread. I am pulling rank.
Shoeb Sattar: please PM me with what you think are the correct answers, in a format like "1a 1c 2b 2c 2d", then I shall reopen the thread and you can post them and we shall all see what we think of them.
He replied saying "B" is correct for the first question.
"B" will compile, but the answer is not complete. this is not an MCQ (where there is one correct and 4 wrong answers) but a true-false, so I wasn't simply being pedantic. There may be more than one correct answer.
When you post a question from a book, mock exam or other source, it is required on JavaRanch that you QuoteYourSources - in other words, please tell us where you copied the question from.
With a question like this it is very easy to compile it and see the answer. If you did compile E then you will learn something about 'weaker access privilege'. That should take you into understanding rules for method overriding.
Incidentally I should have also mentioned that in an interface all the methods are implicitly public and abstract whilst all the variables public static and final. This applies whether you state it or not in the definition.
One of the Java rules about method overriding is that you cannot assign weaker privilege to an overridden method. Imagine people invoked your method in good faith that it implements a public method but only to discover that it isn't so in the implementing class. The compiler will never allow you to break a contract.