Abstract Classes and Interfaces Quiz Quiz

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.

  1. Identifying Abstract Class Usage

    Which scenario best illustrates when you should use an abstract class instead of an interface for a Vehicle base type?

    1. When you do not intend any subclassing
    2. When you want to provide base methods with shared implementation for all vehicles
    3. When you need to ensure multiple inheritance of features
    4. When you only need to define method signatures without any default behavior

    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.

  2. Methods in Interfaces vs Abstract Classes

    Which of the following statements about methods in interfaces is true in most object-oriented languages?

    1. Interfaces can be directly instantiated
    2. All methods in interfaces must be implemented by the implementing class
    3. Abstract classes cannot have abstract methods
    4. Interfaces can contain fully implemented concrete methods only

    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.

  3. Multiple Inheritance and Interfaces

    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?

    1. Only allow Animal to inherit from either Swimmer or Runner
    2. Implement both Swimmer and Runner interfaces in Animal
    3. Define Animal as a final class only
    4. Inherit Animal from both Swimmer and Runner as abstract 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.

  4. Abstract Class Instantiation

    What happens if you attempt to create an instance of an abstract class named Shape in your application?

    1. The instance automatically becomes concrete and can be used
    2. A new Shape object is created with all methods accessible
    3. You receive a compilation or runtime error, depending on the language
    4. Only the abstract methods can be called, not the concrete ones

    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.

  5. Interface vs Abstract Class for Contract Enforcement

    If you want several unrelated classes (such as Printer, Scanner, and WebService) to guarantee a method called connect(), what is the best approach?

    1. Declare a connect() method in an interface and have each class implement it
    2. Declare connect() as a private method in an abstract class common to all
    3. Directly include connect() separately in each class with no contract
    4. Use an abstract class with connect() and require all unrelated classes to inherit it

    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.