Which declare a compilable abstract class? (Choose all that apply.)

  1. public abstract class Canine { public Bark speak(); }

  2. public abstract class Canine { public Bark speak() { } }

  3. public class Canine { public abstract Bark speak(); }

  4. public class Canine abstract { public abstract Bark speak(); }


Correct Option: B

AI Explanation

To answer this question, we need to understand the concept of abstract classes and how they are declared in Java.

An abstract class is a class that cannot be instantiated and is meant to be extended by other classes. It may contain abstract methods, which are methods without any implementation, as well as regular methods with implementations.

Let's go through each option to understand why it is correct or incorrect:

Option A) public abstract class Canine { public Bark speak(); } This option is incorrect because the abstract method "speak()" is not given an implementation. Abstract methods must be implemented in the concrete subclass that extends the abstract class.

Option B) public abstract class Canine { public Bark speak() { } } This option is correct because it declares an abstract class "Canine" with a concrete method "speak()" that returns a "Bark" object. The method implementation can be empty, as long as it is provided.

Option C) public class Canine { public abstract Bark speak(); } This option is incorrect because the class "Canine" is not declared as abstract, but it contains an abstract method "speak()". Abstract methods can only exist in abstract classes.

Option D) public class Canine abstract { public abstract Bark speak(); } This option is incorrect because the keyword "abstract" should come before the class name, not after it. The correct syntax is "public abstract class Canine".

The correct answer is B. This option is correct because it declares an abstract class "Canine" with a concrete method "speak()" that returns a "Bark" object, even though the implementation is empty.

Find more quizzes: