Method Overriding in JAVA

Method Overriding in JAVA Anand

Method Overriding in Java

Method overriding is an important concept in object-oriented programming that allows a subclass to provide its own implementation of a method that is already defined in its parent class. It is mainly used to achieve runtime polymorphism in Java.

When a method in a child class has the same name, return type, and parameters as a method in the parent class, the child class method overrides the parent class method. This allows the child class to modify or extend the behavior of the inherited method.

For students studying the ITI COPA (Computer Operator and Programming Assistant) trade, understanding method overriding is essential because it is widely used in Java applications and helps build flexible and efficient software systems.

What is Method Overriding?

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, parameters, and return type as the method in the superclass.

When an overridden method is called, the version of the method in the subclass is executed instead of the one in the parent class.

Basic Syntax

class ParentClass {

    void display() {
        System.out.println("Parent class method");
    }

}

class ChildClass extends ParentClass {

    void display() {
        System.out.println("Child class method");
    }

}

In this example, the display() method in the child class overrides the display() method in the parent class.

Example Program

class Animal {

    void sound() {
        System.out.println("Animal makes a sound");
    }

}

class Dog extends Animal {

    void sound() {
        System.out.println("Dog barks");
    }

}

public class TestOverride {

    public static void main(String[] args) {

        Dog d = new Dog();
        d.sound();

    }

}

Output:

Dog barks

In this program, the Dog class overrides the sound() method defined in the Animal class.

Rules for Method Overriding

There are several important rules that must be followed when overriding methods in Java.

  • The method name must be the same as the parent class method.
  • The parameter list must be identical.
  • The return type must be the same or compatible.
  • The method must be inherited from the parent class.
  • The overriding method cannot reduce the access level.

These rules help ensure that the subclass method correctly replaces the behavior of the superclass method.

The @Override Annotation

Java provides a special annotation called @Override that helps programmers indicate that a method is overriding a parent class method.

class Animal {

    void eat() {
        System.out.println("Animal eats food");
    }

}

class Dog extends Animal {

    @Override
    void eat() {
        System.out.println("Dog eats meat");
    }

}

The @Override annotation is optional but recommended because it helps the compiler detect errors during compilation.

Using the super Keyword

Sometimes the subclass may need to call the method of the parent class. In such cases, the super keyword is used.

class Animal {

    void eat() {
        System.out.println("Animal eats food");
    }

}

class Dog extends Animal {

    void eat() {
        super.eat();
        System.out.println("Dog eats meat");
    }

}

Here, the child class calls the parent class method using the super keyword.

Difference Between Method Overloading and Method Overriding

FeatureMethod OverloadingMethod Overriding
DefinitionSame method name with different parametersSubclass redefines parent class method
Class LocationSame classDifferent classes
InheritanceNot requiredRequired
Execution TimeCompile timeRuntime

Runtime Polymorphism

Method overriding is used to achieve runtime polymorphism. This means that the method that gets executed is determined during program execution rather than during compilation.

Example:

class Animal {

    void sound() {
        System.out.println("Animal sound");
    }

}

class Cat extends Animal {

    void sound() {
        System.out.println("Cat meows");
    }

}

class Test {

    public static void main(String[] args) {

        Animal a = new Cat();
        a.sound();

    }

}

Output:

Cat meows

Although the reference type is Animal, the method of the Cat class is executed at runtime.

Advantages of Method Overriding

  • Provides runtime polymorphism.
  • Allows modification of inherited behavior.
  • Improves program flexibility.
  • Supports dynamic method dispatch.

These advantages make method overriding an important concept in object-oriented programming.

Applications of Method Overriding

Method overriding is widely used in many software applications.

  • Graphical user interface development
  • Game development
  • Enterprise applications
  • Framework development

Many modern Java frameworks depend heavily on overridden methods.

Importance for ITI COPA Students

For students studying the ITI COPA trade, learning method overriding helps in understanding how classes interact with each other in object-oriented programming.

It allows students to customize inherited methods and develop flexible Java applications.

Understanding method overriding also prepares students for advanced topics such as polymorphism, abstraction, and interface implementation.

Conclusion

Method overriding is a key feature of Java that allows a subclass to redefine the behavior of a method inherited from its parent class. It helps achieve runtime polymorphism and provides flexibility in program design.

By understanding method overriding, programmers can create dynamic and extensible applications. For ITI COPA students, this concept is an important step in mastering object-oriented programming in Java.