Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
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
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
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
Hint
Use super.displayInfo(); to reuse the parent method, then print the model. Create the Car object and call displayInfo() in the main method.
Practice
(1/5)
1. What does the super keyword do in Java?
easy
A. It defines a static method in the class.
B. It creates a new object of the child class.
C. It accesses methods and variables from the parent class.
D. It terminates the program execution.
Solution
Step 1: Understand the role of super
The super keyword is used to refer to the parent class's members (methods or variables) from a child class.
Step 2: Compare options with definition
Only It accesses methods and variables from the parent class. correctly describes this behavior. Other options describe unrelated actions.
Final Answer:
It accesses methods and variables from the parent class. -> Option C
Quick Check:
super accesses parent members = A [OK]
Hint: Remember: super means parent class access [OK]
Common Mistakes:
Thinking super creates new objects
Confusing super with this keyword
Assuming super ends program
2. Which of the following is the correct way to call a parent class constructor in Java?
easy
A. super();
B. this();
C. parent();
D. base();
Solution
Step 1: Recall syntax for parent constructor call
In Java, super() is used inside a child constructor to call the parent class constructor.
Step 2: Evaluate options
Only super(); uses the correct keyword super(). Others are invalid or refer to different concepts.
Final Answer:
super(); -> Option A
Quick Check:
Parent constructor call = super() [OK]
Hint: Use super() to call parent constructor [OK]
Common Mistakes:
Using this() instead of super()
Trying to call parent() which is invalid
Confusing base() with super()
3. What will be the output of the following code?
class Parent {
int x = 10;
}
class Child extends Parent {
int x = 20;
void printX() {
System.out.println(super.x);
}
}
public class Test {
public static void main(String[] args) {
Child c = new Child();
c.printX();
}
}
medium
A. 20
B. Compilation error
C. 0
D. 10
Solution
Step 1: Understand variable hiding and super usage
The child class has its own x = 20, but super.x accesses the parent's x which is 10.
Step 2: Trace the print statement
The method printX() prints super.x, so it prints 10.
Final Answer:
10 -> Option D
Quick Check:
super.x accesses parent variable = 10 [OK]
Hint: super.variable accesses parent class variable [OK]