Java Interface vs Abstract Class: Reasons and Benefits | Exam 1Z0-809

Java Interface vs Abstract Class: Reasons and Benefits

Question

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

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D. E.

BE.

https://books.google.com.br/books?id=nS2tBQAAQBAJ&pg=PT235&lpg=PT235&dq=You+want+to+share+code+among+several+closely+related

Interfaces and abstract classes are both mechanisms for defining contracts in Java, but they have some differences in their usage.

The following are the reasons for using interfaces instead of abstract classes:

  1. You expect that unrelated classes would implement your interfaces.

Interfaces are contracts that define a set of methods that a class must implement, without providing any implementation. This makes them ideal for defining behavior that should be implemented by unrelated classes, especially those in different class hierarchies. Since Java does not support multiple inheritance of implementation (i.e., a class can only extend one other class), using an abstract class in such situations would limit the flexibility of the class hierarchy. Therefore, if you expect unrelated classes to implement your contract, then an interface is a better choice than an abstract class.

  1. You want to take advantage of multiple inheritance of type.

Java does not support multiple inheritance of implementation, but it does support multiple inheritance of type. This means that a class can implement multiple interfaces, but can only extend one class. If you need a class to have multiple types, then an interface is the way to go. An interface can define a set of methods that a class must implement, without imposing any implementation details. This makes interfaces ideal for defining behavior that can be mixed and matched, allowing a class to implement multiple interfaces to achieve the desired behavior.

The following are reasons for using abstract classes instead of interfaces:

  1. You want to share code among several closely related classes.

An abstract class can provide default implementation for methods, which makes it ideal for sharing code among several closely related classes. This is because an abstract class can provide a base implementation for a set of methods, which can be overridden by the subclasses as needed. This allows you to factor out common behavior into a single abstract class, which can be reused by the subclasses.

  1. You expect that classes that implement your interfaces have many common methods or fields, or require access modifiers other than public.

While interfaces can define a set of methods that a class must implement, they cannot define any implementation details. This means that if you have a set of classes that share common methods or fields, an abstract class might be a better choice. This is because an abstract class can provide default implementations for these common methods, which can be overridden by the subclasses as needed. Additionally, an abstract class can provide access modifiers other than public, which can be useful in situations where you want to restrict access to certain methods or fields.

  1. You want to declare non-static on non-final fields.

Interfaces cannot define instance variables, and they can only define constants (i.e., static final variables). If you need a class to have non-static or non-final fields, then an abstract class is the way to go. This is because an abstract class can define instance variables, which can be accessed and modified by the subclasses.