OCP Question 28, Explanation

For which two reasons should you use interfaces instead of abstract classes?

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.
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.

 

The correct answer is BE.

 

Personally, I didn’t meet such a question on the exam but some of my friends did. Besides, this one belongs to the Top 10 questions asked during a Java interview; most likely to be a part of a larger discussion such as “What are the differences between interfaces and abstract classes?”

Anyway, the answer to it can be found in the official Oracle tutorial here:

Consider using interfaces if any of these statements apply to your situation:

  • You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
  • You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
  • You want to take advantage of multiple inheritance of type.

Leave Comment

Your email address will not be published. Required fields are marked *