Java OCA OCP Practice Question 1856

Question

Which statement is true about the following code?.

// Filename: MyClass.java
abstract class MyClass implements Interface1, Interface2 {
  public void f() { }
  public void g() { }
}

interface Interface1 {
  int a = 1;//from w  w  w . j a  v a  2s.c  o  m
  int b = 2;

  void f();
  void g();
}

interface Interface2 {
  int b = 3;
  int c = 4;

  void g();
  void h();
}

Select the one correct answer.

  • (a) MyClass only implements Interface1. Implementation for void h() from Interface2 is missing.
  • (b) The declarations of void g() in the two interfaces conflict, therefore, the code will not compile.
  • (c) The declarations of int b in the two interfaces conflict, therefore, the code will not compile.
  • (d) Nothing is wrong with the code, it will compile without errors.


(e)

Note

The code will compile without errors.

The class MyClass declares that it implements the interfaces Interface1 and Interface2.

Since the class is declared abstract, it does not need to implement all abstract method declarations defined in these interfaces.

Any non-abstract subclasses of MyClass must provide the missing method implementations.

The two interfaces share a common abstract method declaration void g().

MyClass provides an implementation for this abstract method declaration that satisfies both Interface1 and Interface2.

Both interfaces provide declarations of constants named b.

This can lead to an ambiguity when referring to b by its simple name from MyClass.

The ambiguity can be resolved by using fully qualified names: Interface1.b and Interface2.b.




PreviousNext

Related