Way2Java

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.

class Aves
{
  public void nature()
  {
    System.out.println("Aves fly");
  }
}
public class Vulture extends Aves
{
  public void nature()
  {
    System.out.println("Flies very high altitudes");
  }
  public static void main(String args[])
  {
    Vulture v1 = new Vulture();
    v1.nature();
  }
}
Output screen of Vulture.java

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.

class Test
{
  public static void display()
  {
    System.out.println("Hello 1");
  }
}
public class Demo extends Test
{
  public static void display()
  {
    // super.display();         // raises compilation error
    System.out.println("Hello 2");
  }
  public static void main(String args[])
  {
    display();
    Test.display();
  }
}

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.

display();
Test.display();

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.