0
0
Javaprogramming~30 mins

Super keyword in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding the Super Keyword in Java
πŸ“– Scenario: Imagine you are creating a simple program to represent vehicles. You have a basic Vehicle class and a more specific Car class that inherits from Vehicle. You want to use the super keyword to access the parent class's constructor and methods.
🎯 Goal: You will build two classes: Vehicle and Car. You will use the super keyword to call the parent class constructor and method from the child class. Finally, you will print the details of the car using these calls.
πŸ“‹ What You'll Learn
Create a class called Vehicle with a constructor that takes a String parameter called brand and stores it in an instance variable.
Create a method in Vehicle called displayInfo() that prints the vehicle brand.
Create a class called Car that extends Vehicle and has an additional instance variable model.
Use the super keyword in Car's constructor to call the Vehicle constructor.
Override the displayInfo() method in Car and use super.displayInfo() to print the brand before printing the model.
Create an instance of Car and call its displayInfo() method to show the output.
πŸ’‘ Why This Matters
🌍 Real World
Using <code>super</code> is common when you want to build new classes based on existing ones, like creating different types of vehicles or employees with shared and unique features.
πŸ’Ό Career
Understanding inheritance and the <code>super</code> keyword is essential for Java developers to write clean, reusable, and maintainable code in many software projects.
Progress0 / 4 steps
1
Create the Vehicle class with a brand variable and constructor
Create a class called Vehicle with a String instance variable called brand. Add a constructor that takes a String brand parameter and assigns it to the instance variable brand.
Java
Need a hint?

Remember to create a constructor with the same name as the class and assign the parameter to the instance variable.

2
Add a displayInfo() method to Vehicle
Inside the Vehicle class, add a method called displayInfo() that prints the text "Vehicle brand: " followed by the brand variable.
Java
Need a hint?

Use System.out.println to print the brand with the label.

3
Create the Car class that extends Vehicle with a model variable
Create a class called Car that extends Vehicle. Add a String instance variable called model. Create a constructor for Car that takes two String parameters: brand and model. Use the super keyword to call the Vehicle constructor with brand, and assign model to the instance variable.
Java
Need a hint?

Use super(brand); inside the Car constructor to call the parent constructor.

4
Override displayInfo() in Car and print brand and model
In the Car class, override the displayInfo() method. Inside it, first call super.displayInfo() to print the brand. Then print "Car model: " followed by the model variable. Finally, create a Car object with brand "Toyota" and model "Corolla", and call its displayInfo() method.
Java
Need a hint?

Use super.displayInfo(); to reuse the parent method, then print the model. Create the Car object and call displayInfo() in the main method.