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 is a class that cannot be instantiated on its own and is meant to be a base class. It can have abstract methods (without body) that must be implemented by subclasses.
Click to reveal answer
beginner
Can an abstract class have concrete (non-abstract) methods?
Yes, an abstract class can have both abstract methods and concrete methods with full implementations.
Click to reveal answer
beginner
What keyword is used to declare an abstract class and an abstract method in Java?
The keyword <code>abstract</code> is used before the class name to declare an abstract class and before a method signature to declare an abstract method.
Click to reveal answer
beginner
Why can't you create an object of an abstract class?
Because abstract classes are incomplete by design (they may have abstract methods without implementation), Java prevents creating objects from them to avoid errors.
Click to reveal answer
intermediate
How do subclasses relate to abstract classes?
Subclasses must provide implementations for all abstract methods of the abstract class, or they must also be declared abstract.
Click to reveal answer
Which of the following is true about abstract classes in Java?
AYou cannot create an instance of an abstract class.
BAbstract classes cannot have any methods with implementation.
CAbstract classes are the same as interfaces.
DAbstract classes must have at least one abstract method.
✗ Incorrect
Abstract classes cannot be instantiated directly. They can have methods with implementation, and they are different from interfaces. Also, an abstract class can have zero or more abstract methods.
What happens if a subclass does not implement all abstract methods of its abstract superclass?
AThe subclass will compile without errors.
BThe subclass must be declared abstract.
CThe subclass becomes a final class automatically.
DThe program will throw a runtime exception.
✗ Incorrect
If a subclass does not implement all abstract methods, it must also be declared abstract.
Which keyword is used to declare an abstract method?
Aabstract
Bfinal
Cstatic
Dvoid
✗ Incorrect
The keyword abstract is used to declare a method without a body in an abstract class.
Can an abstract class have constructors?
AOnly if all methods are abstract.
BNo, abstract classes cannot have constructors.
CYes, abstract classes can have constructors.
DOnly if the class has no abstract methods.
✗ Incorrect
Abstract classes can have constructors which are called when subclasses are instantiated.
Which statement about abstract classes is false?
AAbstract classes can have fields and methods with implementations.
BAbstract methods must be implemented by subclasses unless the subclass is abstract.
CAbstract classes can be extended by other classes.
DYou can create an object of an abstract class using the new keyword.
✗ Incorrect
You cannot create an object of an abstract class directly using the new keyword.
Explain what an abstract class is and why it is useful in Java.
Think about a blueprint that is incomplete but guides other classes.
You got /5 concepts.
Describe the rules for subclassing an abstract class in Java.
Focus on what the subclass must do with abstract methods.
You got /4 concepts.
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
Step 1: Understand abstract class instantiation
Abstract classes cannot be instantiated directly, meaning you cannot create objects from them using new.
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.
Final Answer:
You cannot create an object directly from an abstract class. -> Option D
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
Step 1: Recall Java syntax for abstract classes
The keyword abstract must come before the keyword class in the declaration.
Step 2: Check each option's order
Only abstract class MyClass {} has the correct order: abstract class MyClass {}. Others have incorrect keyword order.
Final Answer:
abstract class MyClass {} -> Option A
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
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".
Step 2: Call the concrete method from abstract class
Calling sleep() uses the method defined in Animal, printing "Sleeping".
Final Answer:
Bark
Sleeping -> Option C
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
Step 1: Check abstract method implementation in subclasses
The abstract method draw() in Shape must be implemented by all non-abstract subclasses.
Step 2: Verify Square class implementation
The Square class does not implement draw() and is not declared abstract, causing a compilation error.
Final Answer:
Square class must implement the abstract method draw() -> Option A
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
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.
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.
Final Answer:
Make an abstract class Vehicle with an abstract method startEngine(), then subclasses implement it. -> Option B
Quick Check:
Abstract class with abstract method enforces implementation [OK]
Hint: Use abstract method to force subclass-specific behavior [OK]