Method Overloading, this & super
Give one method several versions (overloading), and use this and super to point clearly at the right object.
What you will learn
- Overload a method with different parameter lists
- Use this to refer to the current object and call another constructor
- Use super to reach the parent class
One name, several versions (overloading)
Method overloading means writing several methods with the same name but different parameter lists (a different number or type of inputs). Java picks the right one based on what you pass in. It is handy when an action makes sense for different kinds of input.
Do not confuse it with overriding from the polymorphism lesson. Overriding replaces a parent method in a child (same name, same parameters). Overloading is several same-name methods side by side (same name, different parameters).
public class Printer {
void show(int n) {
System.out.println("Number: " + n);
}
void show(String s) {
System.out.println("Text: " + s);
}
void show(int a, int b) {
System.out.println("Two numbers: " + a + " and " + b);
}
public static void main(String[] args) {
Printer p = new Printer();
p.show(42);
p.show("hello");
p.show(3, 9);
}
}Note: Output:
Number: 42
Text: hello
Two numbers: 3 and 9
All three methods are called show, but they take different inputs. Java looks at what you pass — an int, a String, or two ints — and runs the matching version. That is overloading.
Watch out: Overloaded methods must differ in their parameters (number or types). You cannot overload by only changing the return type — int show() and String show() with the same parameters will not compile, because Java could not tell which one you meant.
this: the current object
You met this in the classes lesson — it means "the object this method belongs to". It has a second, very useful job: this(...) can call another constructor in the same class. This avoids repeating set-up code.
public class Box {
int width, height;
// full constructor
Box(int width, int height) {
this.width = width;
this.height = height;
}
// a "square" shortcut constructor that reuses the one above
Box(int size) {
this(size, size); // calls Box(width, height)
}
public static void main(String[] args) {
Box rect = new Box(4, 2);
Box square = new Box(5);
System.out.println("Rect: " + rect.width + " x " + rect.height);
System.out.println("Square: " + square.width + " x " + square.height);
}
}Note: Output:
Rect: 4 x 2
Square: 5 x 5
The one-argument constructor did not repeat the assignment lines — it called this(size, size), which runs the two-argument constructor. Writing the set-up once and reusing it keeps the code tidy and bug-free.
super: reaching the parent
When one class extends another, super points at the parent. You can use it two ways: super(...) calls the parent constructor, and super.method() calls the parent version of a method even after a child has overridden it.
public class Animal {
String name;
Animal(String name) {
this.name = name;
}
void speak() {
System.out.println(name + " makes a sound.");
}
}
public class Dog extends Animal {
Dog(String name) {
super(name); // run the Animal constructor first
}
@Override
void speak() {
super.speak(); // do the parent's version first
System.out.println(name + " barks: Woof!"); // then add our own
}
public static void main(String[] args) {
Dog d = new Dog("Rex");
d.speak();
}
}Note: Output:
Rex makes a sound.
Rex barks: Woof!
super(name) ran the Animal constructor so the inherited name field was set. Inside the overridden speak(), super.speak() ran the parent line first, then we added the bark. super let us build on the parent instead of replacing it entirely.
this vs super at a glance
| Keyword | Points at | Common uses |
|---|---|---|
this | The current object | this.field (a field), this(...) (another constructor in the same class) |
super | The parent class part | super(...) (parent constructor), super.method() (parent version of a method) |
Tip: A rule Java enforces: if you use this(...) or super(...) to call another constructor, it must be the very first line of the constructor. Java needs the object set up before anything else happens.
Q. What makes two methods overloaded (rather than a clash)?
✍️ Practice
- Write an
addmethod overloaded three ways:add(int, int),add(double, double), andadd(int, int, int). Call all three. - Give a class two constructors, where the shorter one uses
this(...)to call the longer one with default values.
🏠 Homework
- Create a
Vehicleparent with astart()method, and aCarchild that overridesstart()but first callssuper.start()and then prints its own message. Build a Car and callstart().