1Z0-809 practice question 8 of 25
Which two reasons should you use interfaces instead of abstract classes? (Choose two.)
Full set: 196 questionsInterfaces 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.
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.
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."
Keep practicing
Take the free 1Z0-809 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other 1Z0-809 practice questions
- Question 2Given: class Student { String course, name, city; public Student (String name, String cou…
- Question 3Given: class Worker extends Thread { CyclicBarrier cb; public Worker(CyclicBarrier cb) { …
- Question 4Given the code fragment: Path file = Paths.get (“courses.txt”); // line n1 Assume the cou…
- Question 5Given that /green.txt and /colors/yellow.txt are accessible, and the code fragment: Path …
- Question 6Given the code fragment:List<String> empDetails = Arrays.asList(“100, Robin, HR”,“200, Ma…
- Question 7Given the code fragment: List str = Arrays.asList (“my”, “pen”, “is”, “your’, “pen”); Pre…
