1. What Is Method Overriding?
Method overriding happens when a child class creates its own version of a method that already exists in the parent class.
Simple Definition: Method overriding allows a subclass to replace or customize a method from its superclass.
2. Why Do We Use Method Overriding?
Programmers use method overriding when different objects should perform the same action in different ways.
For example, a general Animal class may have a method called makeSound().
Different animals make different sounds:
- A dog barks.
- A cat meows.
- A cow moos.
Each animal can override the makeSound() method with its own version.
3. Parent Class Example
public class Animal
{
public void makeSound()
{
System.out.println("The animal makes a sound.");
}
}
This class has a method called makeSound().
4. Child Class Overriding the Method
public class Dog extends Animal
{
@Override
public void makeSound()
{
System.out.println("The dog barks.");
}
}
The Dog class extends Animal.
The Dog class overrides the makeSound() method.
5. Driver Class Example
public class AnimalDriver
{
public static void main(String[] args)
{
Animal myAnimal = new Animal();
Dog myDog = new Dog();
myAnimal.makeSound();
myDog.makeSound();
}
}
Output
The animal makes a sound.
The dog barks.
6. Important Rules for Overriding
To override a method, the child class method must have:
- The same method name
- The same return type
- The same parameter list
- A relationship using inheritance
public void makeSound()
7. The @Override Annotation
The @Override annotation tells Java that you are trying to override a method.
@Override
public void makeSound()
{
System.out.println("The dog barks.");
}
Why Use @Override?
It helps Java catch mistakes.
@Override is used.
@Override
public void makesound()
{
System.out.println("The dog barks.");
}
This would cause an error because makesound() does not match makeSound().
8. Example With Multiple Child Classes
Parent Class
public class Vehicle
{
public void move()
{
System.out.println("The vehicle moves.");
}
}
Child Class 1
public class Car extends Vehicle
{
@Override
public void move()
{
System.out.println("The car drives on the road.");
}
}
Child Class 2
public class Boat extends Vehicle
{
@Override
public void move()
{
System.out.println("The boat moves through the water.");
}
}
Child Class 3
public class Airplane extends Vehicle
{
@Override
public void move()
{
System.out.println("The airplane flies in the sky.");
}
}
Driver Class
public class VehicleDriver
{
public static void main(String[] args)
{
Vehicle vehicle = new Vehicle();
Car car = new Car();
Boat boat = new Boat();
Airplane airplane = new Airplane();
vehicle.move();
car.move();
boat.move();
airplane.move();
}
}
Output
The vehicle moves.
The car drives on the road.
The boat moves through the water.
The airplane flies in the sky.
9. Method Overriding and Inheritance
Method overriding only works when inheritance is involved.
Inheritance uses the keyword:
extends
Example:
public class Dog extends Animal
This means:
Dogis a child class ofAnimal.Doginherits fromAnimal.Dogcan override methods fromAnimal.
10. Overriding vs. Overloading
| Concept | Meaning |
|---|---|
| Method Overriding | A child class changes a method inherited from a parent class. |
| Method Overloading | Multiple methods have the same name but different parameters. |
Method Overriding Example
public class Parent
{
public void sayHello()
{
System.out.println("Hello from the parent class.");
}
}
public class Child extends Parent
{
@Override
public void sayHello()
{
System.out.println("Hello from the child class.");
}
}
Method Overloading Example
public class Calculator
{
public int add(int a, int b)
{
return a + b;
}
public int add(int a, int b, int c)
{
return a + b + c;
}
}
11. Using super With Overriding
Sometimes, a child class may want to use the parent class version of a method.
To do this, use:
super.methodName();
Example
public class Animal
{
public void makeSound()
{
System.out.println("The animal makes a sound.");
}
}
public class Dog extends Animal
{
@Override
public void makeSound()
{
super.makeSound();
System.out.println("The dog barks.");
}
}
Driver
public class DogDriver
{
public static void main(String[] args)
{
Dog dog = new Dog();
dog.makeSound();
}
}
Output
The animal makes a sound.
The dog barks.
12. Real-World Example
Think about a school.
A general Person class may have a method called work().
public class Person
{
public void work()
{
System.out.println("The person does work.");
}
}
A Teacher works differently from a Student.
public class Teacher extends Person
{
@Override
public void work()
{
System.out.println("The teacher teaches students.");
}
}
public class Student extends Person
{
@Override
public void work()
{
System.out.println("The student completes assignments.");
}
}
13. Complete Program Example
File 1: Person.java
public class Person
{
public void work()
{
System.out.println("The person does work.");
}
}
File 2: Teacher.java
public class Teacher extends Person
{
@Override
public void work()
{
System.out.println("The teacher teaches a lesson.");
}
}
File 3: Student.java
public class Student extends Person
{
@Override
public void work()
{
System.out.println("The student completes classwork.");
}
}
File 4: PersonDriver.java
public class PersonDriver
{
public static void main(String[] args)
{
Person person = new Person();
Teacher teacher = new Teacher();
Student student = new Student();
person.work();
teacher.work();
student.work();
}
}
Output
The person does work.
The teacher teaches a lesson.
The student completes classwork.
14. Common Student Mistakes
Mistake 1: Changing the Method Name
Java is case-sensitive.
// Incorrect
public void MakeSound()
// Correct
public void makeSound()
Mistake 2: Changing the Parameters
// Parent method
public void move()
// Incorrect child method for overriding
public void move(String direction)
This is not overriding. This is overloading.
Mistake 3: Forgetting extends
// Incorrect
public class Dog
// Correct
public class Dog extends Animal
Mistake 4: Forgetting Parentheses
// Incorrect
public void makeSound
// Correct
public void makeSound()
15. Key Vocabulary
| Term | Meaning |
|---|---|
| Parent Class | The class being inherited from. |
| Child Class | The class that inherits from another class. |
| Superclass | Another name for parent class. |
| Subclass | Another name for child class. |
| Override | To replace a parent method with a child version. |
| extends | Java keyword used for inheritance. |
| @Override | Annotation that checks if a method is being overridden correctly. |
| super | Keyword used to access the parent class version of a method. |
16. Check for Understanding
Question 1
What is method overriding?
Answer: Method overriding is when a child class creates its own version of a method that already exists in the parent class.
Question 2
What keyword is used for inheritance in Java?
Answer: extends
Question 3
What annotation is commonly used when overriding a method?
Answer: @Override
Question 4
Can a method be overridden without inheritance?
Answer: No. Method overriding requires inheritance.
Question 5
What does super.methodName() do?
Answer: It calls the parent class version of the method.
17. Summary
Method overriding allows a child class to change the behavior of a method inherited from a parent class.
A child class can use the same method name, return type, and parameters, but write different code inside the method.
Method overriding helps Java programs become more flexible and realistic because different objects can respond to the same method in different ways.
Main Idea
Same method name.
Same parameters.
Different behavior in the child class.