← Quiz: Abstract Classes and Interfaces: all 10 questions
Quiz: Abstract Classes and Interfaces · Question 6 of 10
What does this print? (code)
What does this print?
abstract class Shape {
Shape() { System.out.print("S"); }
abstract int sides();
}
class Triangle extends Shape {
int sides() { return 3; }
}
public class Main {
public static void main(String[] args) {
Shape s = new Triangle();
System.out.println(s.sides());
}
}
- A.3
- B.S3
- C.Compile error: abstract classes cannot have constructors
- D.3S
Answer & explanation
Correct answer: S3
An abstract class can have a constructor. It runs when a subclass object is created, before the subclass's own code.
Can you get the rest right without peeking?
Take the timed Quiz: Abstract Classes and Interfaces quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →