Java extends Keyword

High School Computer Science Class Notes

Student Note

In Java, the keyword extends is used for inheritance.

Inheritance allows one class to reuse code from another class. This helps programmers avoid rewriting the same code over and over again.

1. What Does extends Mean?

The extends keyword means that one class is based on another class.

The new class gets access to the fields and methods of the class it extends.

Basic Format

class ChildClass extends ParentClass
{
    // code for the child class
}

The class after extends is called the parent class or superclass.

The class doing the extending is called the child class or subclass.

2. Parent Class and Child Class

A parent class contains common information or behaviors.

A child class inherits from the parent class and can also add its own features.

Example: Animal Parent Class

public class Animal
{
    public void eat()
    {
        System.out.println("This animal eats food.");
    }
}

Example: Dog Child Class

public class Dog extends Animal
{
    public void bark()
    {
        System.out.println("The dog barks.");
    }
}

The Dog class extends the Animal class.

That means a Dog object can use both eat() and bark().

3. Using the Child Class

public class Main
{
    public static void main(String[] args)
    {
        Dog myDog = new Dog();

        myDog.eat();
        myDog.bark();
    }
}

Output

This animal eats food.
The dog barks.

The Dog class did not write the eat() method, but it can still use it because it inherited it from Animal.

4. Why Use extends?

Programmers use extends to:

5. Real-World Example

Think about vehicles.

A general class could be:

Vehicle

More specific classes could be:

Car
Truck
Motorcycle

All vehicles may have common features like:

speed
start()
stop()

But each specific vehicle may also have its own features.

6. Java Example: Vehicle and Car

Vehicle Class

public class Vehicle
{
    public void start()
    {
        System.out.println("The vehicle starts.");
    }

    public void stop()
    {
        System.out.println("The vehicle stops.");
    }
}

Car Class

public class Car extends Vehicle
{
    public void honk()
    {
        System.out.println("The car honks.");
    }
}

Main Class

public class Main
{
    public static void main(String[] args)
    {
        Car myCar = new Car();

        myCar.start();
        myCar.honk();
        myCar.stop();
    }
}

Output

The vehicle starts.
The car honks.
The vehicle stops.

7. Inheritance Vocabulary

Term Meaning
extends Java keyword used to inherit from another class
Parent class The class being inherited from
Superclass Another name for parent class
Child class The class that inherits
Subclass Another name for child class
Inheritance The process of one class receiving fields and methods from another class

8. The super Keyword

When a child class extends a parent class, it can use the keyword super.

The super keyword refers to the parent class.

Example

public class Animal
{
    public Animal()
    {
        System.out.println("Animal constructor called.");
    }
}
public class Dog extends Animal
{
    public Dog()
    {
        super();
        System.out.println("Dog constructor called.");
    }
}
public class Main
{
    public static void main(String[] args)
    {
        Dog d = new Dog();
    }
}

Output

Animal constructor called.
Dog constructor called.

The parent constructor runs first.

9. Method Overriding

A child class can replace a method from the parent class. This is called method overriding.

Parent Class

public class Animal
{
    public void makeSound()
    {
        System.out.println("The animal makes a sound.");
    }
}

Child Class

public class Dog extends Animal
{
    @Override
    public void makeSound()
    {
        System.out.println("The dog barks.");
    }
}

Main Class

public class Main
{
    public static void main(String[] args)
    {
        Dog myDog = new Dog();
        myDog.makeSound();
    }
}

Output

The dog barks.

The Dog version of makeSound() replaces the Animal version.

10. The @Override Annotation

The @Override annotation tells Java that a method is intended to override a parent class method.

@Override
public void makeSound()
{
    System.out.println("The dog barks.");
}

Using @Override is helpful because Java will give an error if the method does not correctly override a parent method.

11. What Is Inherited?

A child class can inherit:

Important: A child class does not directly access private fields from the parent class.

12. Private Variables and Getters

Even though private variables are not directly accessed by the child class, the child class can use public getter and setter methods.

public class Animal
{
    private String name;

    public Animal(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
}
public class Dog extends Animal
{
    public Dog(String name)
    {
        super(name);
    }

    public void printName()
    {
        System.out.println(getName());
    }
}

The Dog class cannot directly use name, but it can use getName().

13. Important Rule: Java Uses Single Inheritance

In Java, a class can only extend one class.

Valid

public class Dog extends Animal
{
}

Invalid

public class Dog extends Animal, Pet
{
}

A Java class cannot extend two classes at the same time.

14. Common Mistakes

Mistake 1: Trying to Extend Multiple Classes

public class Student extends Person, Athlete
{
}

This is not allowed in Java.

Mistake 2: Forgetting the Parent Constructor

If the parent class has a constructor with parameters, the child class must call it using super().

public class Person
{
    private String name;

    public Person(String name)
    {
        this.name = name;
    }
}
public class Student extends Person
{
    public Student(String name)
    {
        super(name);
    }
}

Mistake 3: Trying to Directly Access Private Variables

public class Animal
{
    private String name;
}
public class Dog extends Animal
{
    public void printName()
    {
        System.out.println(name); // Error
    }
}

The variable name is private, so it cannot be directly used in the child class.

15. Summary

The Java extends keyword is used to create a child class from a parent class.

The child class can reuse methods and fields from the parent class.

Inheritance helps programmers organize code, reduce repetition, and create more specific versions of general classes.

Key Ideas

16. Quick Practice

Question 1

What keyword is used for inheritance in Java?

Answer: extends

Question 2

In this code, which class is the parent class?

public class Dog extends Animal
{
}

Answer: Animal

Question 3

In this code, which class is the child class?

public class Dog extends Animal
{
}

Answer: Dog

Question 4

Can a Java class extend more than one class?

Answer: No. Java only allows a class to extend one class.

Question 5

What does method overriding allow a child class to do?

Answer: It allows the child class to replace a method from the parent class with its own version.