dsa

Java Method Overriding

In this tutorial, we will learn about method overriding in Java with the help of examples.

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding

  • Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
  • Method overriding is used for runtime polymorphism.

Rules for method overriding:

  • Overriding and Access-Modifiers : The access modifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the super-class can be made public, but not private, in the subclass. Doing so, will generate compile-time error.
  • // A Simple Java program to demonstrate 
    // Overriding and Access-Modifiers 
      
    class Parent { 
        // private methods are not overridden 
        private void m1() 
        { 
            System.out.println("From parent m1()"); 
        } 
      
        protected void m2() 
        { 
            System.out.println("From parent m2()"); 
        } 
    } 
      
    class Child extends Parent { 
        // new m1() method 
        // unique to Child class 
        private void m1() 
        { 
            System.out.println("From child m1()"); 
        } 
      
        // overriding method 
        // with more accessibility 
        @Override
        public void m2() 
        { 
            System.out.println("From child m2()"); 
        } 
    } 
      
    // Driver class 
    class Main { 
        public static void main(String[] args) 
        { 
            Parent obj1 = new Parent(); 
            obj1.m2(); 
            Parent obj2 = new Child(); 
            obj2.m2(); 
        } 
    } 
    
    
    Output:
    From parent m2()
    From child m2()
     
  • Final methods can not be overridden : If we don’t want a method to be overridden, we declare it as final. Please see Using final with Inheritance .
  • Static methods can not be overridden(Method Overriding vs Method Hiding) : When you define a static method with same signature as a static method in base class, it is known as method hiding.
  • Private methods can not be overridden : Private methods cannot be overridden as they are bonded during compile time. Therefore we can’t even override private methods in a subclass.
  • The overriding method must have same return type (or subtype) : From Java 5.0 onwards it is possible to have different return type for a overriding method in child class, but child’s return type should be sub-type of parent’s return type. This phenomena is known as covariant return type.
  • Invoking overridden method from sub-class : We can call parent class method in overriding method using super keyword.
  • Overriding and constructor : We can not override constructor as parent and child class can never have constructor with same name(Constructor name must always be same as Class name).
  • Overriding and Exception-Handling : Below are two rules to note when overriding methods related to exception-handling.
    • Rule#1 : If the super-class overridden method does not throw an exception, subclass overriding method can only throws the unchecked exception, throwing checked exception will lead to compile-time error.
    • Rule#2 : If the super-class overridden method does throws an exception, subclass overriding method can only throw same, subclass exception. Throwing parent exception in Exception hierarchy will lead to compile time error.Also there is no issue if subclass overridden method is not throwing any exception.
  • Overriding and abstract method: In Java, abstract classes are created to be the superclass of other classes. And, if a class contains an abstract method, it is mandatory to override it.
  • Overriding and synchronized/strictfp method : The presence of synchronized/strictfp modifier with method have no effect on the rules of overriding, i.e. it’s possible that a synchronized/strictfp method can override a non synchronized/strictfp one and vice-versa.