Method overriding in Java occurs when a subclass provides its own implementation of a method already defined in its parent class, using the same name and parameters. The JVM calls the subclass version at runtime — this is the core of runtime polymorphism and lets you write flexible, reusable object-oriented code.
- Key Takeaway 1: Overriding is resolved at runtime; overloading is resolved at compile time.
- Key Takeaway 2: The
@Overrideannotation is optional but strongly recommended because it catches signature mismatches at compile time. - Key Takeaway 3: Static methods, private methods and final methods cannot be overridden in Java.
- Key Takeaway 4: The
mainmethod cannot be overridden because it is static, but it can be overloaded. - Key Takeaway 5: Covariant return types are allowed since Java 5, meaning the subclass method can return a more specific type than the parent method.
What Method Overriding Is
Method overriding in Java is the ability of a child class to redefine a method inherited from a parent class, keeping the same method name, same parameter list, and same or covariant return type. The Java Virtual Machine (JVM) decides which version to call based on the actual object type at runtime, not the reference type. That decision process is called dynamic method dispatch.
Here is a straightforward example. Suppose you have an Animal class with a sound() method that prints “Some sound”. A Dog subclass overrides sound() to print “Bark”. When you call sound() on an Animal reference pointing to a Dog object, Java prints “Bark”, not “Some sound”. That is runtime polymorphism in action.
Method Overriding in Java with Example: Why It Matters in Real Projects
In a real enterprise application, you might have a base Payment class and subclasses like UPIPayment, CardPayment and NetBankingPayment. Each overrides a processPayment() method. The calling code does not need to know which payment type it is dealing with. This pattern keeps code clean and is used constantly in Java frameworks like Spring. If you are building towards a career in full stack development or software engineering, you will encounter this pattern in almost every production codebase.
According to the Stack Overflow Developer Survey 2024, Java remains one of the top five most-used programming languages globally, used by around 30% of professional developers. According to the JetBrains Developer Ecosystem Survey 2024, Java is the primary language for approximately 27% of developers in the Asia-Pacific region, making OOP concepts like method overriding essential for placement preparation across Indian universities and IT companies. The TIOBE Index for early 2025 still ranks Java in the top three most popular programming languages worldwide, confirming that demand for developers who genuinely understand method overriding in Java is not going anywhere.
In India, method overriding is a core topic in the OOP syllabi of universities including VTU, Anna University, JNTU and Mumbai University, and it appears regularly in GATE CS preparation and campus placement tests at TCS, Infosys, Wipro and Cognizant.
The @Override Annotation
The @Override annotation tells the compiler to verify that the method actually overrides something in the parent class. If you accidentally misspell the method name, the compiler throws an error immediately instead of silently creating a brand-new method. The Oracle Java documentation explicitly recommends always using it.
A common mistake beginners make: they write tostring() instead of toString() and wonder why their output looks wrong. Without @Override, the compiler stays silent. With it, you get an error on the spot.
Rules of Method Overriding in Java
Java enforces a specific set of rules for method overriding. Break any one of them and the code either will not compile or will not behave as expected.
- Same method name and parameter list: The overriding method must have the exact same name and the exact same number, type and order of parameters as the parent method.
- Return type must be the same or covariant: Since Java 5, the subclass can return a subtype of the parent’s return type. For example, if the parent returns Number, the child can return Integer.
- Access modifier cannot be more restrictive: If the parent method is public, the child cannot make it protected or private. You can widen access but never narrow it.
- Cannot override final methods: Declaring a method final in the parent class locks it. No subclass can change it.
- Cannot override static methods: Static methods belong to the class, not the object. What looks like overriding a static method is actually method hiding, a different concept entirely.
- Cannot override private methods: Private methods are not visible to subclasses, so there is nothing to override. The child class simply defines a new method with the same name.
- Checked exceptions can only be narrowed: The overriding method can throw fewer or narrower checked exceptions than the parent, but not broader ones. This rule is closely linked to how Java exceptions and common runtime errors work.
Covariant Return Types in Practice
Covariant return types are useful when building fluent APIs or factory methods. If a parent clone() method returns Object, the subclass can override it to return the specific subclass type. This removes the need for casting on the caller’s side, making the API cleaner and less error-prone.
Difference Between Method Overloading and Overriding in Java
Method overloading and method overriding in Java are both forms of polymorphism, but they work completely differently. Overloading is compile-time polymorphism: you define multiple methods with the same name but different parameter lists in the same class. Overriding is runtime polymorphism: a subclass redefines a method from the parent class with the same signature.
The table below gives a clear, side-by-side comparison of method overloading vs overriding in Java.
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Where it happens | Same class | Parent and child class |
| Parameter list | Must differ | Must be identical |
| Return type | Can differ | Same or covariant |
| Resolved at | Compile time | Runtime |
| Polymorphism type | Static / compile-time | Dynamic / runtime |
| @Override annotation | Not applicable | Recommended |
| Static methods | Can be overloaded | Cannot be overridden (method hiding instead) |
| Private methods | Can be overloaded | Cannot be overridden |
| Exception handling | No restriction | Cannot throw broader checked exceptions |
| Inheritance required | No | Yes |
If you are studying BSc Computer Science subjects and scope or preparing for placements after an MCA, this comparison will come up repeatedly in written tests and technical rounds. To see how these fundamentals apply across languages, check out how to write, compile and run a C program for a useful look at compiled language basics.
Can the main Method Be Overridden in Java?
No. The main method in Java cannot be overridden because it is declared static. Static methods belong to the class itself, not to any object instance. Overriding is a runtime, object-level concept. Static methods do not participate in dynamic dispatch at all.
What you can do is overload the main method. You can write main(String[] args) and also main(int x) in the same class. Both are valid Java. But the JVM will always call public static void main(String[] args) as the entry point. If you write a main method in a child class with the same signature as the parent’s main, you are not overriding it. You are hiding it. No runtime polymorphism is involved.
This distinction is tested frequently in GATE preparation and technical interviews. If you are planning your career path after an MCA and want to know which Java roles pay well, the jobs and salary after MCA guide has solid data on that.
Before you explore overriding further, it helps to have a clear grasp of the software development tools to learn first, since a good IDE like IntelliJ IDEA or Eclipse will highlight overriding errors and suggest the @Override annotation automatically.
Frequently Asked Questions
What is method overriding in Java?
Method overriding in Java is when a subclass defines a method with the same name, same parameters and the same or covariant return type as a method in its parent class. Java calls the subclass version at runtime based on the actual object type. This is the foundation of runtime polymorphism in Java.
What is the difference between method overloading and overriding in Java?
Overloading happens in the same class and is resolved at compile time: multiple methods share a name but differ in parameters. Overriding happens across a parent-child class relationship and is resolved at runtime: the child redefines the parent’s method with the same signature. They serve different purposes and should never be confused in an interview or production code.
What are the rules of method overriding in Java?
The overriding method must have the same name and parameter list as the parent method. The return type must be the same or a covariant subtype. The access modifier cannot be made more restrictive. Private, static and final methods cannot be overridden. The overriding method cannot declare broader checked exceptions than the parent method.
Can we override the main method in Java?
No, you cannot override the main method because it is static, and static methods are not subject to runtime polymorphism. Writing a main method in a subclass with the same signature is called method hiding, not overriding. The JVM always calls the class-specific static main method directly based on which class you run.
Why should you use the @Override annotation?
The @Override annotation instructs the Java compiler to verify that the method actually overrides a parent class method. If you accidentally misspell the method name or change the parameter list, the compiler throws an error immediately. Without it, the compiler treats the method as a new, unrelated method and gives no warning, which leads to subtle, hard-to-find bugs.
Mastering method overriding in Java gives you the foundation to write flexible, maintainable object-oriented code that scales. Here are three things you can do this week to lock in this knowledge:
- Write a small Java program with a parent class Shape and child classes Circle, Rectangle and Triangle, each overriding an area() method. Run it and observe dynamic dispatch in action.
- Add @Override to every method you intend to override and deliberately misspell one method name to see the compile-time error. That hands-on experience makes the rule stick.
- Explore how overriding connects to Java exception handling by testing what happens when you throw different exception types in an overriding method.
Strong object-oriented programming fundamentals are the bedrock of secure coding, AI development and ethical hacking. If you want to build on these skills and move into cybersecurity or AI, 3.0 University’s CEH v13 AI online course is a practical next step. You can also browse the full catalogue of 3.0 University online certification courses in Cybersecurity, Ethical Hacking, AI, Blockchain and Web3 to find the right path for your goals.
Last updated: June 2025. Reviewed by the 3University editorial team.