How does an abstract class differ from an interface in Java?

Study for the Java Technical Interview! Test your knowledge with multiple choice questions, each offering hints and explanations. Get ready to ace your Java exam!

Multiple Choice

How does an abstract class differ from an interface in Java?

Explanation:
An abstract class can indeed have concrete methods, meaning that it can provide implementations for some methods, while also allowing abstract methods that must be implemented by subclasses. This feature gives an abstract class a level of flexibility similar to a regular class, where it can have both complete and incomplete behavior. In contrast, an interface in Java traditionally cannot have any method implementations until Java 8, when default methods were introduced. This means that, primarily, interfaces serve as a pure contract for what methods a class should implement without providing any of the actual implementations. The other choices do have inaccuracies that set them apart from the distinction between abstract classes and interfaces. For instance, stating that both can have fields is misleading since, while abstract classes can have member variables, interfaces can only have constants (which are implicitly public, static, and final). The assertion that an abstract class can only be extended while interfaces can be implemented does not capture the nuances of how interfaces can also extend other interfaces. Lastly, interfaces cannot have constructors, whereas abstract classes can have them, allowing for additional initialization when instances of subclasses are created.

An abstract class can indeed have concrete methods, meaning that it can provide implementations for some methods, while also allowing abstract methods that must be implemented by subclasses. This feature gives an abstract class a level of flexibility similar to a regular class, where it can have both complete and incomplete behavior.

In contrast, an interface in Java traditionally cannot have any method implementations until Java 8, when default methods were introduced. This means that, primarily, interfaces serve as a pure contract for what methods a class should implement without providing any of the actual implementations.

The other choices do have inaccuracies that set them apart from the distinction between abstract classes and interfaces. For instance, stating that both can have fields is misleading since, while abstract classes can have member variables, interfaces can only have constants (which are implicitly public, static, and final). The assertion that an abstract class can only be extended while interfaces can be implemented does not capture the nuances of how interfaces can also extend other interfaces. Lastly, interfaces cannot have constructors, whereas abstract classes can have them, allowing for additional initialization when instances of subclasses are created.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy