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
Recall & Review
beginner
What is an abstract class in Java?
An abstract class in Java is a class that cannot be instantiated directly. It can have abstract methods (without body) that must be implemented by subclasses, and it can also have concrete methods with implementations.
Click to reveal answer
beginner
What is a concrete class in Java?
A concrete class in Java is a class that can be instantiated. It provides implementations for all its methods and can be used to create objects.
Click to reveal answer
beginner
Can you create an object of an abstract class directly?
No, you cannot create an object of an abstract class directly. You must create an object of a subclass that extends the abstract class and implements its abstract methods.
Click to reveal answer
intermediate
Why use abstract classes instead of concrete classes?
Abstract classes are used to define a common template or blueprint for subclasses. They allow you to enforce certain methods to be implemented while sharing common code, promoting code reuse and design clarity.
Click to reveal answer
intermediate
What happens if a concrete class does not implement all abstract methods from its abstract superclass?
If a concrete class does not implement all abstract methods from its abstract superclass, the concrete class must also be declared abstract. Otherwise, the code will not compile.
Click to reveal answer
Which of the following statements is true about abstract classes in Java?
AAbstract classes are the same as interfaces.
BAbstract classes cannot have any methods with implementation.
CYou cannot create an instance of an abstract class.
DAbstract classes must be final.
✗ Incorrect
Abstract classes cannot be instantiated directly, but they can have methods with implementation.
What must a concrete subclass do when it extends an abstract class?
ADeclare itself abstract.
BHave no constructors.
COverride all concrete methods of the abstract class.
DImplement all abstract methods of the abstract class.
✗ Incorrect
A concrete subclass must implement all abstract methods inherited from the abstract class.
Which keyword is used to declare an abstract class in Java?
Aconcrete
Babstract
Cstatic
Dfinal
✗ Incorrect
The keyword 'abstract' is used to declare an abstract class.
Can a concrete class extend an abstract class?
AYes, and it must implement all abstract methods.
BNo, only abstract classes can extend abstract classes.
CYes, but it cannot add new methods.
DNo, concrete classes cannot extend any class.
✗ Incorrect
Concrete classes can extend abstract classes but must implement all abstract methods.
If a class has at least one abstract method, what must be true?
AThe class must be declared abstract.
BThe class must be final.
CThe class must be concrete.
DThe class must have no constructors.
✗ Incorrect
Any class with abstract methods must be declared abstract.
Explain the difference between abstract and concrete classes in Java.
Think about whether you can create objects and if methods have bodies.
You got /4 concepts.
Describe why and when you would use an abstract class instead of a concrete class.
Consider design and code reuse benefits.
You got /4 concepts.
Practice
(1/5)
1. Which statement best describes an abstract class in Java?
easy
A. It is the same as an interface and cannot have any methods with code.
B. It must have all methods fully implemented and can be instantiated.
C. It can have methods without implementation and cannot be instantiated directly.
D. It is a class that can only contain static methods.
Solution
Step 1: Understand abstract class definition
An abstract class can have methods without implementation (abstract methods) and cannot create objects directly.
Step 2: Compare with other options
Concrete classes have full method implementations and can be instantiated. Interfaces differ from abstract classes. Static-only classes are unrelated.
Final Answer:
It can have methods without implementation and cannot be instantiated directly. -> Option C
2. Which of the following is the correct way to declare an abstract class in Java?
easy
A. abstract class Vehicle {}
B. class abstract Vehicle {}
C. Vehicle abstract class {}
D. class Vehicle abstract {}
Solution
Step 1: Recall Java syntax for abstract classes
The keyword 'abstract' comes before 'class' followed by the class name.
Step 2: Check each option
Only 'abstract class Vehicle {}' matches correct syntax. The other options have incorrect keyword order.
Final Answer:
abstract class Vehicle {} -> Option A
Quick Check:
abstract keyword before class name [OK]
Hint: Put 'abstract' 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 Java code?
abstract class Animal {
abstract void sound();
}
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();
}
}
medium
A. Runtime error
B. Animal sound
C. Compilation error
D. Bark
Solution
Step 1: Understand class hierarchy and method overriding
Animal is abstract with abstract method sound(). Dog extends Animal and implements sound() printing "Bark".
Step 2: Analyze main method execution
Animal reference points to Dog object. Calling a.sound() runs Dog's sound(), printing "Bark".
Final Answer:
Bark -> Option D
Quick Check:
Abstract method overridden = Dog's output [OK]
Hint: Abstract method calls run subclass implementation [OK]
Common Mistakes:
Expecting abstract class method to run
Thinking abstract class can be instantiated
Confusing compile and runtime errors
4. Identify the error in the following code snippet:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
// No draw() method implemented
}
public class Test {
public static void main(String[] args) {
Circle c = new Circle();
c.draw();
}
}
medium
A. Circle must implement the abstract method draw() or be declared abstract.
B. Cannot create object of class Circle.
C. Abstract class Shape cannot have abstract methods.
D. No error, code runs fine.
Solution
Step 1: Check subclass implementation of abstract methods
Circle extends Shape but does not implement abstract method draw().
Step 2: Understand Java rules for abstract methods
A concrete class must implement all abstract methods or be declared abstract itself. Circle is concrete but missing draw().
Final Answer:
Circle must implement the abstract method draw() or be declared abstract. -> Option A
Quick Check:
Concrete subclass must implement all abstract methods [OK]
Hint: Concrete class must implement all abstract methods [OK]
Common Mistakes:
Thinking abstract methods can be skipped
Assuming abstract class can't have abstract methods
Believing object creation is the error
5. You want to design a system where different types of employees calculate their salary differently. Which approach best uses abstract and concrete classes?
hard
A. Create only concrete classes for each employee type without any abstract class.
B. Create an abstract class Employee with an abstract method calculateSalary(), then create concrete subclasses like Manager and Developer implementing it.
C. Use an interface with no methods and concrete classes implementing it.
D. Create a concrete Employee class with a fixed calculateSalary() method used by all employees.
Solution
Step 1: Identify need for shared rules with different implementations
Employee types share concept of salary calculation but differ in details.
Step 2: Use abstract class with abstract method
Abstract class Employee defines calculateSalary() abstractly. Subclasses implement specific logic.
Step 3: Evaluate other options
Create only concrete classes for each employee type without any abstract class. lacks shared abstraction. Use an interface with no methods and concrete classes implementing it. uses interface with no methods, so no contract. Create a concrete Employee class with a fixed calculateSalary() method used by all employees. fixes salary calculation, no variation.
Final Answer:
Create an abstract class Employee with an abstract method calculateSalary(), then create concrete subclasses like Manager and Developer implementing it. -> Option B
Quick Check:
Abstract class sets rules, subclasses do work [OK]
Hint: Abstract class for rules, concrete classes for details [OK]
Common Mistakes:
Not using abstraction for shared behavior
Using concrete class with fixed method only
Interfaces without methods don't enforce contracts