extends & super
Use extends to reuse a base class, and super to call the parent constructor or methods. Keep examples tiny and readable.
extends & super is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is inheritance?
Inheritance lets a class reuse and extend another class. The child gets the parent's fields and methods, and may add its own. Use it to share common code across similar types.
Using extends
Use the extends keyword to create a subclass. Example idea: class Car extends Vehicle. The subclass is a specific kind of the parent and can reuse its behavior.
super(...) in constructors
Inside a child constructor, super(...) calls the parent constructor. This initializes the parent part first. If you do not call it explicitly, Java calls the parent's no-arg constructor by default (if it exists).
super.method()
You can also write super.someMethod() to use the parent's version of a method. This is handy when the child wants to add small extra behavior around the base logic.
When to use it
Use inheritance for a clear is-a relation: a Car is a Vehicle. If you only want to reuse functionality without an is-a fit, prefer composition. Keep class trees shallow for beginners.
Code: extends & super
Code: Car extends Vehicle. The constructor calls super to set the brand. The child method calls super.printInfo() and then adds its own line.
public class Main {
// Base class with a simple constructor and method
static class Vehicle {
String brand;
Vehicle(String brand) {
this.brand = brand; // parent sets its own field
}
void printInfo() {
System.out.println("Vehicle brand: " + brand);
}
}
// Subclass reuses Vehicle with extends
static class Car extends Vehicle {
int doors;
Car(String brand, int doors) {
super(brand); // call parent constructor first
this.doors = doors;
}
void printInfo() {
// Add extra info, but reuse parent behavior
super.printInfo(); // call parent method
System.out.println("Doors: " + doors);
}
}
public static void main(String[] args) {
// Create a Car (which is a Vehicle)
Car c = new Car("EcoMotors", 4);
c.printInfo(); // shows parent info then child info
}
}
super(...) check
Quick check: What does the super(...) call do inside a subclass constructor?
Recap
Recap: extends sets up an is-a relation; super(...) initializes the parent, and super.method() reuses base logic. Prefer shallow, simple hierarchies.
Frequently asked questions
Is the “extends & super” lesson free?
Yes — the full text of “extends & super” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “extends & super”?
Use extends to reuse a base class, and super to call the parent constructor or methods. Keep examples tiny and readable. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “extends & super” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- extends & super
- Method Overriding
- Polymorphism & Dynamic Binding