Bird
Raised Fist0
Javaprogramming~20 mins

Abstract classes in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
πŸŽ–οΈ
Abstract Class Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of abstract class method call
What is the output of this Java program that uses an abstract class and a subclass?
Java
abstract class Animal {
    abstract void sound();
}

class Dog extends Animal {
    void sound() {
        System.out.println("Woof");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
AAnimal
BRuntime error: NullPointerException
CCompilation error: Cannot instantiate abstract class
DWoof
Attempts:
2 left
πŸ’‘ Hint
Look at which class is instantiated and which method is called.
🧠 Conceptual
intermediate
1:30remaining
Why use abstract classes?
Which of the following is the main reason to use an abstract class in Java?
ATo create objects directly without subclassing
BTo allow multiple inheritance of implementation
CTo provide a common base with some shared code and force subclasses to implement specific methods
DTo improve runtime performance by avoiding method calls
Attempts:
2 left
πŸ’‘ Hint
Think about what abstract classes enforce and what they provide.
πŸ”§ Debug
advanced
2:00remaining
Identify the compilation error
What error does this code produce when compiled?
Java
abstract class Vehicle {
    abstract void start();
}

class Car extends Vehicle {
    void start() {
        System.out.println("Car started");
    }
}

class Bike extends Vehicle {
}

public class Test {
    public static void main(String[] args) {
        Vehicle v = new Bike();
        v.start();
    }
}
ARuntime error: NullPointerException
BCompilation error: Bike must implement abstract method start()
CCompilation error: Cannot instantiate abstract class Vehicle
DNo error, output: Car started
Attempts:
2 left
πŸ’‘ Hint
Check if all abstract methods are implemented in subclasses.
πŸ“ Syntax
advanced
1:30remaining
Correct abstract method declaration
Which option shows the correct way to declare an abstract method in an abstract class?
Java
abstract class Shape {
    // method declaration here
}
Aabstract void draw();
Bvoid abstract draw();
Cabstract void draw() {}
Dvoid draw();
Attempts:
2 left
πŸ’‘ Hint
Abstract methods have no body and use the 'abstract' keyword before the return type.
πŸš€ Application
expert
2:00remaining
Number of objects created
Given the code below, how many objects are created when main runs?
Java
abstract class Fruit {
    Fruit() {
        System.out.println("Fruit created");
    }
}

class Apple extends Fruit {
    Apple() {
        System.out.println("Apple created");
    }
}

public class Main {
    public static void main(String[] args) {
        Fruit f = new Apple();
    }
}
AOne object: an Apple instance
BTwo objects: one Fruit and one Apple
CNo objects, only references
DCompilation error: Cannot instantiate abstract class Fruit
Attempts:
2 left
πŸ’‘ Hint
Remember how constructors work in inheritance and abstract classes.

Practice

(1/5)
1. Which statement about abstract classes in Java is true?
easy
A. All methods in an abstract class must be abstract.
B. Abstract classes cannot have any methods with code.
C. Abstract classes are the same as interfaces.
D. You cannot create an object directly from an abstract class.

Solution

  1. Step 1: Understand abstract class instantiation

    Abstract classes cannot be instantiated directly, meaning you cannot create objects from them using new.
  2. Step 2: Check method rules in abstract classes

    Abstract classes can have both abstract methods (without body) and regular methods (with code). So, not all methods must be abstract.
  3. Final Answer:

    You cannot create an object directly from an abstract class. -> Option D
  4. Quick Check:

    Abstract class instantiation = not allowed [OK]
Hint: Remember: abstract classes can't make objects directly [OK]
Common Mistakes:
  • Thinking abstract classes can be instantiated
  • Believing all methods must be abstract
  • Confusing abstract classes with interfaces
2. Which of the following is the correct way to declare an abstract class in Java?
easy
A. abstract class MyClass {}
B. class abstract MyClass {}
C. abstract MyClass class {}
D. class MyClass abstract {}

Solution

  1. Step 1: Recall Java syntax for abstract classes

    The keyword abstract must come before the keyword class in the declaration.
  2. Step 2: Check each option's order

    Only abstract class MyClass {} has the correct order: abstract class MyClass {}. Others have incorrect keyword order.
  3. Final Answer:

    abstract class MyClass {} -> Option A
  4. Quick Check:

    abstract class syntax = 'abstract class' [OK]
Hint: abstract keyword always before class keyword [OK]
Common Mistakes:
  • Placing abstract after class
  • Mixing keyword order
  • Omitting abstract keyword
3. What will be the output of the following code?
abstract class Animal {
    abstract void sound();
    void sleep() {
        System.out.println("Sleeping");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
        a.sleep();
    }
}
medium
A. Sleeping\nBark
B. Compilation error
C. Bark\nSleeping
D. Runtime error

Solution

  1. Step 1: Understand method calls on abstract class reference

    The variable a is of type Animal but refers to a Dog object. Calling sound() calls Dog's implementation, printing "Bark".
  2. Step 2: Call the concrete method from abstract class

    Calling sleep() uses the method defined in Animal, printing "Sleeping".
  3. Final Answer:

    Bark Sleeping -> Option C
  4. Quick Check:

    Dog sound then Animal sleep = Bark then Sleeping [OK]
Hint: Abstract ref calls subclass method, regular method runs as is [OK]
Common Mistakes:
  • Expecting compilation error for abstract class reference
  • Confusing method call order
  • Thinking abstract class methods can't be called
4. Identify the error in the following code:
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}

class Square extends Shape {
}

public class Test {
    public static void main(String[] args) {
        Shape s = new Square();
        s.draw();
    }
}
medium
A. Square class must implement the abstract method draw()
B. Cannot create object of abstract class Shape
C. Method draw() in Circle should be abstract
D. No error, code runs fine

Solution

  1. Step 1: Check abstract method implementation in subclasses

    The abstract method draw() in Shape must be implemented by all non-abstract subclasses.
  2. Step 2: Verify Square class implementation

    The Square class does not implement draw() and is not declared abstract, causing a compilation error.
  3. Final Answer:

    Square class must implement the abstract method draw() -> Option A
  4. Quick Check:

    All abstract methods must be implemented in concrete subclasses [OK]
Hint: All abstract methods must be implemented or class must be abstract [OK]
Common Mistakes:
  • Forgetting to implement abstract methods
  • Thinking abstract class objects can be created
  • Marking implemented methods as abstract
5. You want to design a system where different types of vehicles share a common method startEngine() but each vehicle starts differently. Which approach using abstract classes is best?
hard
A. Make Vehicle a concrete class with startEngine() implemented, subclasses override it if needed.
B. Make an abstract class Vehicle with an abstract method startEngine(), then subclasses implement it.
C. Make Vehicle an interface with startEngine() method, implemented by subclasses.
D. Make Vehicle a final class with startEngine() method.

Solution

  1. Step 1: Understand the need for shared method with different implementations

    Since startEngine() must be shared but implemented differently, an abstract method enforces subclasses to provide their own version.
  2. Step 2: Choose abstract class with abstract method

    Declaring Vehicle as abstract with abstract startEngine() ensures all subclasses implement it, sharing the concept but customizing behavior.
  3. Final Answer:

    Make an abstract class Vehicle with an abstract method startEngine(), then subclasses implement it. -> Option B
  4. Quick Check:

    Abstract class with abstract method enforces implementation [OK]
Hint: Use abstract method to force subclass-specific behavior [OK]
Common Mistakes:
  • Using concrete method without forcing override
  • Confusing interfaces with abstract classes
  • Making class final prevents subclassing