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
- super(args) calls parent constructor
- Must be first line in child constructor
- super.method() calls parent method
Related Inheritance exercises
- Practice Basic Inheritance in Java
- Practice Method Overriding in Java
- Practice Abstract Classes in Java
Practice all Inheritance exercises · Run this idea in the Java compiler