Challenge - 5 Problems
Abstract Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of abstract class method call
What is the output of the following Java code?
Java
abstract class Animal { abstract void sound(); } class Dog extends Animal { void sound() { System.out.println("Bark"); } } public class Main { public static void main(String[] args) { Animal a = new Dog(); a.sound(); } }
Attempts:
2 left
π‘ Hint
Look at which class is instantiated and which method is called.
β Incorrect
The abstract class Animal cannot be instantiated directly, but Dog extends Animal and provides implementation for the abstract method sound(). The object a is of type Animal but refers to a Dog instance, so Dog's sound() method is called, printing "Bark".
β Predict Output
intermediate2:00remaining
What happens when instantiating an abstract class?
What error does the following code produce?
Java
abstract class Vehicle { abstract void move(); } public class Main { public static void main(String[] args) { Vehicle v = new Vehicle(); v.move(); } }
Attempts:
2 left
π‘ Hint
Can you create an object of an abstract class directly?
β Incorrect
Abstract classes cannot be instantiated directly. Trying to create an object of Vehicle causes a compilation error.
β Predict Output
advanced2:00remaining
Output with abstract and concrete class methods
What is the output of this Java program?
Java
abstract class Shape { void display() { System.out.println("Shape display"); } abstract void draw(); } class Circle extends Shape { void draw() { System.out.println("Circle draw"); } void display() { System.out.println("Circle display"); } } public class Main { public static void main(String[] args) { Shape s = new Circle(); s.display(); s.draw(); } }
Attempts:
2 left
π‘ Hint
Which display() method is called? The one in Shape or Circle?
β Incorrect
The object s is of type Shape but refers to a Circle instance. The display() method is overridden in Circle, so Circle's display() runs. The draw() method is abstract in Shape and implemented in Circle, so Circle's draw() runs.
β Predict Output
advanced2:00remaining
Which option causes a compilation error?
Given the abstract class and subclass below, which option will cause a compilation error?
Java
abstract class Device { abstract void start(); } class Phone extends Device { void start() { System.out.println("Phone starting"); } } public class Main { public static void main(String[] args) { // Options below } }
Attempts:
2 left
π‘ Hint
Can you create an instance of an abstract class?
β Incorrect
Option C tries to instantiate the abstract class Device directly, which is not allowed and causes a compilation error. The other options create instances of Phone, which is concrete.
π§ Conceptual
expert2:00remaining
Why use abstract classes instead of concrete classes?
Which of the following is the best reason to use an abstract class instead of a concrete class in Java?
Attempts:
2 left
π‘ Hint
Think about what abstract classes allow you to do with methods.
β Incorrect
Abstract classes let you define some common behavior and force subclasses to implement specific methods. They cannot be instantiated directly. This helps design flexible and reusable code.