Section 13.5 Check Point Questions5 questions
13.5.1
Suppose A is an interface. Can you create an instance using new A()?
13.5.2
Suppose A is an interface. Can you declare a reference variable x with type A like this?
A x;
13.5.3
Which of the following is a correct interface?
(a) interface A { void print() { } } (b) abstract interface A { abstract void print() { } } (c) abstract interface A { print(); } (d) interface A { void print(); } (e) interface A { default void print() { } } (f) interface A { static int get() { return 0; } }
13.5.4
Show the error in the following code:
interface A { void m1(); } class B implements A { void m1() { System.out.println("m1"); } }
13.5.5
The following questions are based on the Edible
interface and the classes defined in LiveExample 13.7 and
also assume LittleChicken is a subtype of Chicken.
For each question, answer if the code can compile, can run. If not, give
a reason. If it runs, give the output.
a. Edible x = new Tiger(); b. Edible x = new Chicken(); System.out.println(x.sound()); c. Edible x = new Chicken(); System.out.println((Animal)x.sound()); d. Edible x = new Chicken(); System.out.println(((Animal)x).sound()); e. Edible x = new LittleChicken(); System.out.println(x.howToEat()); f. LittleChicken x = new Chicken();