Q) Which is true statements about an interface in java?
- An interface can extend another interface
- We can create object of an interface in java
- An interface can have constructor
- None
Answer: 1
An interface can extend another interface or multiple interfaces.
Q) What is output of below java program?
interface X
{
void f();
}
interface Y extends X
{
void g();
}
class C implements Y
{
public void f() {
System.out.println("Hello");
}
public void g() {
System.out.println("World");
}
}
public class Main {
public static void main(String[] args) {
C o = new C();
o.f();
o.g();
}
}
Output:
A)
Hello
World
B) Hello
C) World
D Compiler error
Answer: A
Confusion point: Can an interface extends another interface? Answer is yes, and just a class can implement first interface. The extended interface methods will be available.
Q) Which of the following contains only unimplemented methods?
- Class
- Abstract class
- Interface
- None
Answer: 3
Java interfaces only contains unimplemented abstract methods. These all methods are implemented by the class who implements the interface.
Q) A class inherits an interface using which keyword?
- Extends
- Implements
- Inherit
- None
Answer: 2
A class inherits an interface using implements keyword. For example,
class Y implements X, where X is an interface.