What can an abstract class do that an interface cannot?

Prepare for your SDET Interview with comprehensive flashcards and challenging multiple-choice questions. Each question is designed with hints and detailed explanations to ensure your success. Start your journey to mastering the SDET Interview today!

Multiple Choice

What can an abstract class do that an interface cannot?

Explanation:
The key distinction between an abstract class and an interface lies in the ability of an abstract class to both declare methods and provide their implementations. An abstract class can contain concrete methods, which are fully defined with a body, allowing it to encapsulate shared functionality that can be utilized by derived classes. This means that while an interface can only declare methods without any implementation (prior to certain language updates), an abstract class supports providing default behavior, which can then be optionally overridden by subclasses. This feature enables more flexibility in the design of software, as common behavior can be centralized within the abstract class, reducing code duplication. In contrast, interfaces are limited to what they can express, focusing primarily on defining a contract that implementing classes must follow without any specific behavior attached. Regarding the context of why other options are not correct: while multiple inheritance is a characteristic associated with interfaces in some languages, abstract classes themselves do not have inherent multiple inheritance unless the language supports it with specific rules; private members can exist in abstract classes, whereas interfaces traditionally cannot enforce access modifiers in the same way; and interfaces are implemented using the "implements" keyword, which distinguishes that behavior from how classes extend abstract classes.

The key distinction between an abstract class and an interface lies in the ability of an abstract class to both declare methods and provide their implementations. An abstract class can contain concrete methods, which are fully defined with a body, allowing it to encapsulate shared functionality that can be utilized by derived classes. This means that while an interface can only declare methods without any implementation (prior to certain language updates), an abstract class supports providing default behavior, which can then be optionally overridden by subclasses.

This feature enables more flexibility in the design of software, as common behavior can be centralized within the abstract class, reducing code duplication. In contrast, interfaces are limited to what they can express, focusing primarily on defining a contract that implementing classes must follow without any specific behavior attached.

Regarding the context of why other options are not correct: while multiple inheritance is a characteristic associated with interfaces in some languages, abstract classes themselves do not have inherent multiple inheritance unless the language supports it with specific rules; private members can exist in abstract classes, whereas interfaces traditionally cannot enforce access modifiers in the same way; and interfaces are implemented using the "implements" keyword, which distinguishes that behavior from how classes extend abstract classes.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy