Loading...

Java Inheritance

Make a child type with extends so it can use a method written on the parent. Put horn on Car so it prints beep. Write class Truck extends Car. In Yard.java, build a Truck and call t.horn(). Compile with javac Yard.java and run java Yard. The screen should show beep. A second demo also builds a plain Car so you see the parent still works after the child exists. Leave super for the next page. Do not rewrite horn on Truck on this page — overriding waits later.

A child keeps a parent method

The paths page named places with Paths.get. This page starts a new module: one class can be a child of another. Open a teaching file named Yard.java. Keep the public class name Yard so the file name matches. Delete leftover Route path demos, leftover Store writers, leftover Reader scanners, and any keyboard Scanner. The program must not wait for typing. You need no import line for this lesson.
Above the public class, declare a package-private parent type:
class Car {
Inside Car, write an instance method void horn() that prints beep:
System.out.println("beep");
Then declare the child:
class Truck extends Car {
Keep the Truck body empty on this page. The child does not rewrite horn. Empty braces are enough to say Truck is a kind of Car.
Inside Yard.main, build a truck and call the inherited method:
Truck t = new Truck();
t.horn();
The call works because extends Car connects Truck to Car. You did not copy horn into Truck.

Yard Truck extends Car horn beep

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

class Truck extends Car {
}

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. Only one public class sits in this file, so the file name matches Yard. The helper types Car and Truck stay without public on this beginner shape. Putting three public classes in one file would make javac refuse — keep that rule from the classes page.
The screen shows beep. Walk the run once. Java starts at Yard.main. new Truck() builds a truck object. t.horn() finds horn on the parent Car through inheritance. The method prints beep. If javac says it cannot find horn on Truck, the extends Car line is missing or misspelled — put it back. If you see note.txt or a path print, a Route leftover is still launching — switch to java Yard. If the terminal waits for typing, a keyboard Scanner leftover is still in the file — delete it.
Spell extends carefully. A comma or a mistaken implements word is not this lesson. Inheritance here is the extends keyword between child and parent type names. The child name comes first: Truck extends Car, not the reverse order.
Do not write super on this page. Calling the parent constructor with super is the next lesson. Do not write a second horn inside Truck either; that override story comes later. Today the child stays empty and still uses the parent method. That empty body is intentional teaching, not a mistake to “fix” by copying horn down into Truck.

📊 Parent and child on this page

Piece Role
Car parent type that owns horn
Truck child type via extends Car
Yard public starter with main
horn prints beep; written only on Car

The parent still works on its own

A second program keeps the same Car, the same empty Truck extends Car, and the same public Yard. Inside main, build both a Car and a Truck, and call horn on each.

Yard Car and Truck both horn

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

class Truck extends Car {
}

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 beep again. The first line comes from the plain Car. The second line comes from the Truck that inherited horn. Making a child did not delete or break the parent type. Both objects can call the same method body written once on Car. You still build each object yourself with new; the extends line does not create a truck for you automatically.
That contrast is the checkpoint. Inheritance adds a child path to an existing method. It does not require you to remove the parent. If you remove extends Car from Truck, then t.horn() fails to compile, because Truck no longer receives horn. Keep extends for the teaching child. Prefer the single-truck file when you want one quiet beep. Prefer the two-object file when you want to prove the parent still answers.

Compared with paths on Route

On the paths page, java Route named folders and files. On this page, java Yard connects types with extends. Keep Route files only as history if they still sit nearby; run java Yard for the inheritance demos so the screen matches this lesson.
extends, parent method reuse, and an empty child body are the new tokens. You already know new and instance methods from earlier OOP pages. Today those pieces meet inheritance for the first time in this course path. Keep the teaching names Car, Truck, and Yard so your compile and run commands stay short and clear.

What this leaves for super

This page shows a child that uses a parent method without writing super. The next page introduces super so a child can call into the parent on purpose, especially around constructors. Stay with extends and the inherited horn call here. Do not add super() as a requirement on these Yard demos yet.

Before super

You can write Truck extends Car, build a Truck, and see beep from horn. You can also build a plain Car and see the parent still print beep. Compile Yard.java and run java Yard for each kept demo in turn. The child stayed empty and still used the parent method. The next page is super, where the child talks back to the parent more directly.
Keep the single truck demo for a clear inherited call. Keep the two-object demo to prove the parent still works on its own. Keep super off this page for now. Those three habits are the checkpoint before you move on. When beep appears from a Truck that never wrote its own horn, inheritance did its job for this lesson and you are ready for the next topic about super.

🧠 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