posted 15 years ago
2. Given:
1. interface Animal {
2. void eat();
3. }
4.
5. // insert code here
6.
7. public class HouseCat implements Feline {
8. public void eat() { }
9. }
And the following three interface declarations:
interface Feline extends Animal { }
interface Feline extends Animal { void eat(); }
interface Feline extends Animal { void eat() { } }
How many, inserted independently at line 5, will compile?
1 - 0
2 - 1
3 - 2
4 - 3
The Answer is = 3
Explanation: An interface doesn't necessary have to implements methods, so knowing that you can say ..
interface Feline extends Animal { } /* it doesn't implement the Animal methods but is fine since its an interface */
interface Feline extends Animal { void eat(); } /* implements the method eat () */
BUT if you say
interface Feline extends Animal { void eat() { } } /* we have an error here INTERFACE METHODS CAN'T HAVE A BODY, can't have "{}", so it's wrong */