Bird
0
0

You have two classes:

hard📝 Application Q8 of 15
Java - Inheritance
You have two classes:
class Vehicle {
  String type = "Vehicle";
  void printType() {
    System.out.println(type);
  }
}
class Car extends Vehicle {
  String type = "Car";
  void printType() {
    System.out.println(type);
    System.out.println(super.type);
  }
}

How can you modify the Car class constructor to call the parent constructor and print "Vehicle created" before printing types?
AAdd <code>super.printType(); System.out.println("Vehicle created");</code> in Car constructor
BAdd <code>this(); System.out.println("Vehicle created");</code> in Car constructor
CAdd <code>super(); System.out.println("Vehicle created");</code> as first lines in Car constructor
DAdd <code>Vehicle(); System.out.println("Vehicle created");</code> in Car constructor
Step-by-Step Solution
Solution:
  1. Step 1: Call parent constructor explicitly

    Use super(); as first statement in Car constructor to call Vehicle constructor.
  2. Step 2: Print message after super call

    Print "Vehicle created" after calling super(); inside Car constructor.
  3. Final Answer:

    Add super(); System.out.println("Vehicle created"); as first lines in Car constructor -> Option C
  4. Quick Check:

    super() must be first in constructor [OK]
Quick Trick: super() must be first in constructor to call parent [OK]
Common Mistakes:
  • Using this() instead of super()
  • Calling parent constructor like a method
  • Printing before super() call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes