Using Super Keyword in Java: Explanation & Practice

Call parent constructor and methods using super

Problem summary

Create a Vehicle and Car class where Car calls Vehicle's constructor using super.

Starter code

// Create Vehicle with:
// - String brand
// - constructor(brand)
// - void info() prints brand

// Create Car extending Vehicle:
// - int year
// - constructor(brand, year) using super

public class Main {
    public static void main(String[] args) {
        // Create car and print info
    }
}

Expected output and test cases

  • Car info
    Toyota 2023

Hints

  1. super(args) calls parent constructor
  2. Must be first line in child constructor
  3. super.method() calls parent method

Related Inheritance exercises

Practice all Inheritance exercises · Run this idea in the Java compiler