Explore key concepts of abstract classes and interfaces with practical scenarios. This quiz helps reinforce understanding of inheritance, implementation, method signatures, and design patterns relevant to abstract classes and interfaces.
Which scenario best illustrates when you should use an abstract class instead of an interface for a Vehicle base type?
Explanation: An abstract class is ideal when you need to provide some common implementation to all subclasses, ensuring code reuse and consistency. Interfaces are suitable if you only need to declare method signatures without shared code. Multiple inheritance is not supported by abstract classes but can be emulated with interfaces in some languages. If you aren't subclassing at all, a concrete class would suffice rather than an abstract class or interface.
Which of the following statements about methods in interfaces is true in most object-oriented languages?
Explanation: Typically, all abstract methods declared in interfaces must be implemented by the class that implements the interface, unless the language supports default methods (which is an exception and not the default). Interfaces cannot generally contain fully implemented concrete methods, though some languages have limited support for default methods. Abstract classes often have abstract methods. Interfaces themselves cannot be instantiated directly, as they lack implementation.
Given a class Animal that should inherit behavior from both Swimmer and Runner, which language construct would allow this if the language does not support multiple inheritance of classes?
Explanation: Implementing multiple interfaces is a common way to achieve multiple inheritance of behavior where class-based multiple inheritance is not allowed. Inheriting from two abstract classes is not permitted in many languages. Declaring Animal as final prevents any subclassing, and limiting inheritance to just one type would prevent the intended combined behavior. Interfaces allow flexible code design for such cases.
What happens if you attempt to create an instance of an abstract class named Shape in your application?
Explanation: Most languages prevent instantiation of abstract classes and will raise an error at compile time or runtime. Creating a regular Shape object is not allowed because the abstract class might contain unimplemented methods. The notion that only certain types of methods are accessible is incorrect; you cannot instantiate the class at all. Abstract classes do not automatically convert to concrete classes upon instantiation.
If you want several unrelated classes (such as Printer, Scanner, and WebService) to guarantee a method called connect(), what is the best approach?
Explanation: Interfaces are designed to enforce contracts across unrelated classes and ensure all implement specific methods like connect(). An abstract class works best for related types; forcing unrelated classes to inherit from the same abstract class limits flexibility and may not even be possible if there isn’t a common ancestor. A private abstract method cannot be implemented by subclasses. Defining methods separately offers no enforced contract, making interfaces the most reliable choice.