Method Overriding Java

Method Overriding Java

Subclass can make use of super class methods straight away by virtue of inheritnace. If the subclass does not satisfy about the functionality (output) of the super class method, the subclass can rewrite with its own functionality with the same method name. This concept is known as "method overriding", supported by OOP languages like C++/Java. In method overriding, super class and subclass have the method with the same signature – same parameters and same return type.

Let us discuss rules of method overriding with the following example.

Method Overriding Java

Output screen of

In the above program, Aves and Vulture classes have the same method nature() with their own functionalities (outputs).

If the super class and subclass have the same method, the subclass calls its own method. By this rule, the subclass object, v1, calls its own overridden method.

The method overloading and method overriding sounds similar; but they differ completely.

Method Overloading vs Method Overriding
Method Overloading Method Overriding
1. Happens in the same class Happens among two classes – a super class and a subclass
2. No inheritance Happens in inheritance only
3. One method does not hide another Subclass method hides super class method
4. Should have different parameters Should have same parameters
5. Return type is not bothered Should have the same return type
6. Leads to static binding and static polymorphism Leads to dynamic binding, dynamic polymorphism and dynamic method dispatch

Table: Differences: Overloading vs Overriding

Java Static Inheritance

Here, one rule to remember in inheritance is "static members of a class cannot be overridden".

Why the static members cannot be overridden?

Generally, any member (variable or method) when overridden by the subclass, the subclass can call the super class member with "super" keyword. But "static member" cannot be called as with static members "this" reference cannot be used because static members can be called without the help of an object. When static members are overridden, the compiler does not raise error as compiler treats both are different belonging to their own class.

The above explanation will be clearer with the following program.

The display() method is static in the super class Test and also in subclass Demo. Compiler does not treat it as method overriding as it feels each display() method belong to it's own class.

// super.display();

"super" cannot be used with static members and raises compilation error.


The display() declared in Demo class calls its own and Test.display() calls Test class display() method. No ambiguity in understanding by the compiler.

Final conclusion: Static members of a class cannot be overridden and "super" keyword cannot be used with static members (say, to call super class static method from subclass static method).

Note: Private members of a class cannot be overridden. But public, protected and default members can be overridden.

15 thoughts on “Method Overriding Java

  1. Mobin Shaik

    Hai sir, i want to know
    how the memory is allocated for overriden method ?
    i mean whether the method in super class and overriden method in subclass points to same memory location or A new memory is created for overriden method ??

    thank you in advance : )

  2. Sunita Bansal

    sir, Recently I was asked this question “why should one override a method? ”
    I replied, if I have a class with 10 methods and I want to use all of its functionality except one method, then I will override that method to have my own functionality.
    Then the interviewer replied in that case why cant we write a new method with a different name and use that method instead.
    Yes this is also right. Now I am confused. What is the real objective in overriding a method?
    Can anyone please tell me? Thank you all in advance.

    1. Yagna Valli

      Well Sunita, it’s useful when we want to update the class, whic can be done in child class in inheritance concept. ope this clears your doubt.

  3. vijay

    sir,in method overriding,after inheritance,the the derived class have access to members of base class.So, new memory locations will be created for base class members which have been inherited in derived class?.

  4. Ravi D

    Hi Sir,
    I am a big fan of your site.
    I think Method overloading happens in super class and sub class relation also.
    If I am correct,Please correct the first point.

    package org.oops;
    class Aves
    public void nature()
    System.out.println(“Aves fly”);
    public class Vulture extends Aves
    public void nature(String name)
    System.out.println(“Flies very high altitudes”);
    public static void main(String args[])
    Vulture v1 = new Vulture();

      1. Ravi D

        Aves a1 = new Vulture();
        This code should call the Overridden method(sub class method).
        But its calling the super class method.
        In my above code No method Overridden happened.
        instead of its overloaded the method between the sub and super classes.

Leave a Reply

Your email address will not be published. Required fields are marked *