LearnFrenzy
Sign in Take a free test

Constructors and the this Keyword · Find the Error · Question 9

Error: calling both this(...) and super(...) Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Vehicle {
    Vehicle() { }
}

class Car extends Vehicle {
    String model;
    Car(String model) { this.model = model; }
    Car() {
        super();
        this("Default");
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Car().model);
    }
}
  • A Vehicle must be abstract
  • B A constructor can call either this(...) or super(...) as its first statement, not both Correct
  • C model must be final
  • D Car cannot extend Vehicle
Correct Answer: Option B

Car(String) already calls super() implicitly; Car() only needs this("Default").

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links