1Z0-809 practice question 8 of 25

Which two reasons should you use interfaces instead of abstract classes? (Choose two.)

Full set: 196 questions
Answer options
A: You expect that classes that implement your interfaces have many common methods or fields, or require access modifiers other than public.
B: You expect that unrelated classes would implement your interfaces. Correct answer
C: You want to share code among several closely related classes.
D: You want to declare non-static on non-final fields.
E: You want to take advantage of multiple inheritance of type. Correct answer
Correct Answer:
B. You expect that unrelated classes would implement your interfaces.; E. You want to take advantage of multiple inheritance of type.
Explanation

Interfaces are used to define a contract that unrelated classes can implement. This allows objects of different class hierarchies to be treated polymorphically based on the shared interface. For example, String and Integer are unrelated but both implement Comparable. Additionally, a class can implement multiple interfaces, which allows it to inherit multiple types, a form of multiple inheritance that is not possible with classes. Abstract classes are better suited for sharing code and state among closely related classes within a single inheritance hierarchy.

Why Incorrect

A. This describes a use case for abstract classes, which can have fields, non-public methods, and shared implementation code.
C. This is the primary purpose of an abstract class, which serves as a base for a hierarchy of related classes.
D. Interfaces can only declare public static final fields. Abstract classes can declare non-static and non-final fields.

References

1. Oracle, The Java™ Tutorials. "Abstract Methods and Classes". "Consider using abstract classes if... You want to share code among several closely related classes."

2. Oracle, The Java™ Tutorials. "Interfaces". "Consider using interfaces if... You expect that unrelated classes would implement your interface... You want to take advantage of multiple inheritance of type."

3. The Java™ Language Specification, Java SE 8 Edition, §9.1. "An interface declaration introduces a new reference type... A class can implement more than one interface."

Scroll to Top