Loading...

Java Overriding

Rewrite a parent method on the child so the call uses the child’s body. Keep Truck extends Car. On Car, keep horn printing beep. On Truck, write horn again so it prints honk. In Yard.java, build a Truck and call t.horn(). Compile with javac Yard.java and run java Yard. The screen should show honk. A second demo also builds a plain Car so you hear beep from the parent and honk from the child. Leave interfaces for the next page — this lesson is one method name with two bodies.

Rewrite horn on the child

The inheritance page left Truck empty so t.horn() kept the parent beep. The super page ran constructors with super. This page changes the method body on the child. Open Yard.java. Keep the public class name Yard. Delete leftover constructor-only prints of car and truck if they bury the method story, leftover path demos, leftover Store writers, and any keyboard Scanner. The program must not wait for typing. No import line is required.
Declare the parent with an instance method that prints beep:
class Car {
void horn() {
System.out.println("beep");
}
}
Declare the child with extends Car. Inside Truck, write the same method name again, but print honk:
void horn() {
System.out.println("honk");
}
Matching name and matching empty parameter list mean the child version replaces the parent version for Truck objects. You are not adding a second unrelated method. You are overriding horn.
Inside Yard.main, build one truck and call the method:
Truck t = new Truck();
t.horn();
The call runs the body written on Truck, so the screen prints honk.

Yard Truck overrides horn to honk

java
class Car {
    void horn() {
        System.out.println("beep");
    }
}

class Truck extends Car {
    void horn() {
        System.out.println("honk");
    }
}

public class Yard {
    public static void main(String[] args) {
        Truck t = new Truck();
        t.horn();
    }
}

From the folder that holds Yard.java, run javac Yard.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Yard — not java Yard.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Yard. Helper types Car and Truck stay without public on this beginner shape.
The screen shows honk. Walk the run once. Java starts at Yard.main. new Truck() builds a truck. t.horn() looks at the real type of t. Because Truck wrote its own horn, that child body runs and prints honk. The parent beep body stays on Car and is not used for this truck call.
If you still see beep, check whether Truck really contains a horn method. An empty Truck from the inheritance page still inherits beep. Overriding requires the child method to be present with the same name. If you still see car then truck from a constructor demo, switch to the override teaching file and run java Yard again.
Say the rule out loud while you watch the screen: same method name on the child, child body wins for that object. Keep extends Car so Truck stays a kind of Car. Without extends, a horn on Truck would be a separate type story, not an override of the parent method.
Do not invent an interface file on this page. Interfaces wait for the next lesson. Today one class hierarchy carries two horn bodies, and the child’s body answers the call on a truck.

Parent beep and child honk side by side

A second program keeps the override. main builds both a Car and a Truck, then calls horn on each. The parent still prints beep. The child still prints honk.

Yard Car beep and Truck honk

java
class Car {
    void horn() {
        System.out.println("beep");
    }
}

class Truck extends Car {
    void horn() {
        System.out.println("honk");
    }
}

public class Yard {
    public static void main(String[] args) {
        Car c = new Car();
        Truck t = new Truck();
        c.horn();
        t.horn();
    }
}

Compile with javac Yard.java, then run java Yard. The screen shows beep, then honk. The first line comes from the Car object. The second line comes from the Truck object. Overriding on the child does not erase the parent method for parent objects. Each object uses the body that belongs to its own type.
Read the two horn methods once more. Same name. Same empty parameter list. Different print text. That sameness of signature is what makes the child version an override instead of a new unrelated method. If you rename the child method to something else, inheritance would keep beep on trucks again, and you would no longer be teaching override.
If leftover constructor super() demos still sit in the folder, keep them as history. For this lesson run the override teaching file so the screen shows method prints, not constructor order words. If javac complains about duplicate public classes, you still have more than one public class in the file — keep only Yard public.

Parent body versus child body

📊 Who prints what for horn

Role horn body
Parent Car prints beep
Child Truck prints honk (overrides)
The table is the short map of this page. Inheritance alone would put beep on both roles when the child stays empty. After overriding, the child row changes to honk while the parent row stays beep. Keep that map in mind when the second demo prints both lines.

Compared with empty extends and with super

On the inheritance page, empty Truck kept parent horn and printed beep. On the super page, constructors ran through super and printed car then truck. On this page, the method itself changes on the child. You still need extends. You still may use constructors later. You do not need an interface yet.
If a leftover empty Truck is still open, add the child horn that prints honk before you judge the screen. If a leftover constructor file is still open, switch to the override file so t.horn() is the call under test. The new tokens are the rewritten method on the child and the two different screen words beep and honk.

Before interfaces

You can rewrite horn on Truck and see honk from t.horn(). You can call both c.horn() and t.horn() and see beep then honk. Compile Yard.java and run java Yard for each kept demo in turn. The child body answered the truck call. The next page is interfaces, where a type can promise methods without using extends alone.
Keep the single-truck honk demo for a clear override. Keep the two-object demo for a clear parent-versus-child contrast. Keep interface files off this page. Those three habits are the checkpoint before you move on. When honk appears after t.horn() and beep still appears after c.horn(), overriding did its job for this lesson and you are ready for the next topic about interfaces.
One last pass through the folder: one public Yard, two helper types, a child horn that prints honk, no keyboard wait, no forced interface. Run javac Yard.java and java Yard once more on the demo you keep open. If the screen still matches the table — parent beep, child honk — the override is solid and the course can move to interfaces on the next page.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top