Overriding in Java: Step-by-Step Explanation | Updated 2025

Method Overriding in Java: Concepts, Rules, and Examples

CyberSecurity Framework and Implementation article ACTE

About author

Dharani (Web Developer )

Dharani is a Web Developer who focuses on runtime polymorphism and object-oriented programming, particularly method overriding for customizing dynamic behavior. She is an expert in dynamic method dispatch, inheritance hierarchies, and the @Override annotation. She teaches how subclasses modify superclass methods to customize functionality.

Last updated on 16th Sep 2025| 10754

(5.0) | 32961 Ratings

Introduction to Method Overriding

Method Overriding in Java is a key concept in object-oriented programming (OOP). It allows a subclass to provide a specific implementation of a method that is already defined in its superclass. When a method in a subclass has the same name, return type, and parameters as a method in the superclass, the method in the subclass overrides the method in the superclass. To complement this object-oriented concept with practical development expertise, exploring Web Developer Training reveals how mastering technologies like HTML, CSS, JavaScript, and backend frameworks empowers professionals to build scalable web applications applying principles like inheritance and overriding in real-world project architectures. This enables dynamic (runtime) polymorphism and allows for more flexible and reusable code. Overriding is central to Java’s philosophy of providing customizable and extendable behavior.


To Earn Your Web Developer Certification, Gain Insights From Leading Data Science Experts And Advance Your Career With ACTE’s Web Developer Courses Today!


Rules for Overriding in Java

Overriding in Java, properly overriding a method involves following specific guidelines to ensure functionality and avoid errors. First, the overriding method must match the name of the method in the superclass. It also needs to have the same parameter list, which includes the type, number, and order of parameters. The return type can be the same or a subtype, this is called a covariant return type. Additionally, the access level of the overriding method should not be more restrictive than that of the overridden method. To complement these object-oriented principles with practical output control, exploring Python String Formatting reveals how techniques like f-strings, `.format()`, and template strings allow developers to produce readable, dynamic output enhancing clarity and maintainability in method overrides and runtime diagnostics. Importantly, the method in the superclass cannot be private, static, or final, as these types of methods cannot be overridden. Furthermore, the overriding method must not throw any new or broader checked exceptions than those specified by the overridden method. By following these rules, developers can ensure correct method overriding, preventing unexpected runtime behavior or compilation errors and maintaining code reliability.

    Subscribe To Contact Course Advisor

    Superclass vs Subclass Methods

    • In the context of overriding, the superclass contains the original method, while the subclass redefines that method with its own implementation. When a subclass object is accessed via a superclass reference, the overridden method in the subclass gets called, thanks to Java’s runtime polymorphism.
    • This behavior helps in decoupling method behavior from the method call, allowing a parent class reference to execute different subclass behaviors dynamically. However, if a method is declared as static, it is not eligible for overriding but rather hidden, leading to compile-time method binding.

    • Would You Like to Know More About Web Developer? Sign Up For Our Web Developer Courses Now!


      Use of @Override Annotation

      Java provides the @Override annotation to ensure that a method is correctly overriding a method in its superclass. This annotation is not mandatory, but it is highly recommended. If a developer mistakenly declares a method that does not actually override a method from its parent class, the compiler will flag it as an error. To complement this safeguard in object-oriented programming with data persistence techniques, exploring Python Serialization reveals how Python’s `pickle`, `json`, and `marshal` modules enable developers to convert objects into storable formats ensuring seamless data exchange, version control, and state management across distributed systems.

      • class Animal {
      • void sound() {
      • System.out.println(“Animal makes a sound”);
      • }
      • }
      • class Dog extends Animal {
      • @Override
      • void sound() {
      • System.out.println(“Dog barks”);
      • }
      • }

      Here, @Override ensures that sound() in Dog overrides the method in Animal.

      Course Curriculum

      Develop Your Skills with Web Developer Certification Course

      Weekday / Weekend BatchesSee Batch Details

      Runtime Polymorphism

      Method overriding enables runtime polymorphism in Java. In this approach, a parent class reference is used to refer to a child class object, and the method call is resolved at runtime. To complement this dynamic behavior in object-oriented programming with practical development skills, exploring Web Developer Training reveals how mastering technologies like HTML, CSS, JavaScript, and backend frameworks equips professionals to build responsive, scalable web applications applying core principles like inheritance and polymorphism in real-world development environments.

      • Animal obj = new Dog();
      • obj.sound(); // Outputs: Dog barks

      Although the reference is of type Animal, the sound() method of the Dog class is invoked because the object is of type Dog. This late binding mechanism enhances flexibility and enables the implementation of behaviors like callbacks and strategy patterns.


      Are You Interested in Learning More About Web Developer? Sign Up For Our Web Developer Courses Today!


      Examples and Syntax

      • class Vehicle {
      • void move() {
      • System.out.println(“Vehicle is moving”);
      • }
      • }
      • class Car extends Vehicle {
      • @Override
      • void move() {
      • System.out.println(“Car is moving”);
      • }
      • }
      • public class Main {
      • public static void main(String[] args) {
      • Vehicle obj = new Car();
      • obj.move(); // Output: Car is moving
      • }
      • }

      This example shows how a method in the subclass (Car) overrides the superclass method (Vehicle) and how runtime polymorphism directs the method call. To complement this object-oriented behavior with process-level reliability, exploring What is Quality Assurance. reveals how systematic QA practices ensure that software development processes from design to deployment adhere to defined standards, reducing defects and improving overall product quality.

      Web Development Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

      Method Overloading vs Overriding

      While method overriding and method overloading may sound similar, they are distinctly different concepts in Java: overriding allows a subclass to provide a specific implementation of a method already defined in its superclass, while overloading enables multiple methods with the same name but different parameters. To complement these object-oriented techniques with performance validation strategies, exploring What is Spike Testing reveals how sudden surges or drops in user load are simulated to evaluate system stability, recovery time, and responsiveness under extreme conditions.

      • Method Overriding: Occurs between a superclass and subclass with identical method signatures. It enables runtime polymorphism and allows a subclass to provide a specific implementation of a method already defined in its parent class.
      • Method Overloading: Happens within the same class, where multiple methods share the same name but differ in parameter types, number, or order. It supports compile-time polymorphism and enhances method flexibility.
      • class MathUtils {
      • int add(int a, int b) { return a + b; }
      • double add(double a, double b) { return a + b; } // Overloaded method
      • }

      Overriding supports polymorphism, whereas Method Overloading enhances code readability and reusability.

      Access Modifiers and Exceptions

      Access modifiers are important in method overriding in object-oriented programming. When a public method in a superclass is overridden in a subclass, it must also be public. A protected method can be overridden in the subclass as protected or public. However, private methods cannot be overridden at all. Besides access modifiers, exception handling during overriding has specific rules. To complement these object-oriented constraints with career-level insights, exploring Full Stack Developer Salary reveals how mastering both frontend and backend technologies can lead to high-paying roles especially for developers who understand scalable architectures, robust coding practices, and cross-functional responsibilities in modern software teams. The overriding method cannot throw broader checked exceptions than those declared in the method it overrides. It can throw narrower or fewer checked exceptions or even switch to unchecked exceptions. Understanding these guidelines helps maintain proper functionality and consistency in your code.

      • class Parent {
      • void read() throws IOException {
      • // Some code
      • }
      • }
      • class Child extends Parent {
      • @Override
      • void read() throws FileNotFoundException { // Valid override
      • // Some code
      • }
      • }

      Abstract and Interface Methods

      Method overriding is important when working with Abstract and Interface in programming. Abstract classes can have abstract methods, which are promises that any specific subclass must fulfill by providing implementations. This makes sure that all subclasses have the necessary features specified by the abstract class. Similarly, interfaces provide method signatures that implementing classes must override. To complement these object-oriented design principles with service-level integration strategies, exploring WSDL in Web Services reveals how WSDL acts as a formal contract between client and server defining available operations, data types, and communication protocols to ensure seamless interoperability across distributed systems. This ensures that any class claiming to follow that interface will provide its own version of the methods. By using method overriding, programmers can create more flexible and reusable code while following a clear structure set by abstract classes and interfaces.

      • abstract class Shape {
      • abstract void draw();
      • }
      • class Circle extends Shape {
      • @Override
      • void draw() {
      • System.out.println(“Drawing Circle”);
      • }
      • }
      • interface Printer {
      • void print();
      • }
      • class Document implements Printer {
      • @Override
      • public void print() {
      • System.out.println(“Printing Document”);
      • }
      • }

      In both scenarios, overriding is necessary to provide specific behaviors.


      Advantages of Overriding

      Overriding provides several advantages:

      • Flexibility: Allows subclasses to tailor or extend the behavior of superclass methods.
      • Polymorphism: Enables dynamic method invocation.
      • Code Reusability: Avoids code duplication by extending existing logic.
      • Custom Implementations: Ensures that each subclass can provide its own unique implementation.
      • Improved Maintainability: Centralizes method behavior while allowing extensions.

      These advantages support object-oriented principles such as encapsulation, inheritance, and polymorphism.


      Common Mistakes

      Despite its simplicity, developers often make errors while overriding methods: common mistakes include mismatched method signatures, incorrect access modifiers, or overlooking the use of the @Override annotation. To complement these object-oriented programming challenges with hands-on engineering tools, exploring Virtual Instrumentation using Labview reveals how graphical programming environments like LabVIEW empower developers to design, simulate, and test real-world systems bridging software logic with hardware control through intuitive visual workflows.

      • Incorrect Method Signature: Not matching the exact signature (name and parameters).
      • Changing Return Type Inappropriately: Using a non-compatible return type.
      • Throwing Broader Exceptions: Violating exception rules.
      • Using static, final, or private: These methods cannot be overridden.
      • Forgetting @Override: Missing the annotation can lead to undetected mistakes.

      Ensuring careful adherence to overriding rules avoids such issues.


      Summary

      Method Overriding in Java is a cornerstone of object-oriented architecture, enabling developers to customize inherited behavior for specific subclass implementations. By redefining methods, Java applications become more dynamic, maintainable, and adaptable. Through concepts such as runtime polymorphism, use of annotations, proper access control, and exception handling, overriding empowers developers to build robust and scalable applications. To complement these object-oriented principles with practical web development expertise, exploring Web Developer Training reveals how mastering technologies like HTML, CSS, JavaScript, and backend frameworks equips professionals to create responsive, secure, and scalable web applications that align with modern software engineering standards. Understanding and applying method overriding effectively is essential for any Java programmer aiming to create flexible and reusable object-oriented designs.

    Upcoming Batches

    Name Date Details
    Web Developer Certification Course

    15 - Sep- 2025

    (Weekdays) Weekdays Regular

    View Details
    Web Developer Certification Course

    17 - Sep - 2025

    (Weekdays) Weekdays Regular

    View Details
    Web Developer Certification Course

    20 - Sep - 2025

    (Weekends) Weekend Regular

    View Details
    Web Developer Certification Course

    21 - Sep - 2025

    (Weekends) Weekend Fasttrack

    View Details