๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Method Overriding in Java  


Introduction
  • Method Overriding is a way (or mechanism or form) to achieve runtime polymorphism
  • It is a feature in Java that allows the child class to write its own implementation of a method that is already present in the parent class.
  • The JVM determines which method to execute at runtime, based on the object type (not the reference type).
  • Rules of Method Overriding :-
    • The methods must have the same name.
    • The method must be in different class or in subclass.
    • All the method parameters list must be same:
      • Number of parameters
      • Type of parameters
      • Order of parameters
    • Should follow IS-A relationship (Inheritance).
  • Example :-
    class Bank
    {
        double getInterestRate()
        {
            return 0.0;
        }
    }
    
    class SBI extends Bank
    {
        @Override
        double getInterestRate()
        {
            return 6.5;
        }
    }
    
    class HDFC extends Bank
    {
        @Override
        double getInterestRate()
        {
            return 7.0;
        }
    }
    
    class ICICI extends Bank
    {
        @Override
        double getInterestRate()
        {
            return 6.8;
        }
    }
    
    public class Main
    {
        public static void main(String[] args)
        {
            Bank b1 = new SBI();
            Bank b2 = new HDFC();
            Bank b3 = new ICICI();
    
            System.out.println("SBI Interest Rate: " + b1.getInterestRate() + "%");
            System.out.println("HDFC Interest Rate: " + b2.getInterestRate() + "%");
            System.out.println("ICICI Interest Rate: " + b3.getInterestRate() + "%");
        }
    }
    Output:
    SBI Interest Rate: 6.5%
    HDFC Interest Rate: 7.0%
    ICICI Interest Rate: 6.8%

Advantages of Method Overriding :-
  1. Runtime Polymorphism::
    • Allows Java to decide at runtime which method to execute, enabling flexible and dynamic behavior.
  2. Improves Code Reusability:
    • Reuses method names from the parent class while allowing customized behavior in the child class.
  3. Supports Inheritance:
    • Strengthens the use of inheritance by enabling subclasses to modify or enhance the behavior of parent class methods.
  4. Better Readability and Maintainability:
    • Makes the code more logical and organized when different classes handle behavior in their own way.
  5. Encourages Consistency::
    • Keeps method names the same across class hierarchies, promoting consistency in design.
  6. Useful in Frameworks & Libraries::
    • Commonly used in Java frameworks (like Spring, Hibernate) where parent classes define default behavior and subclasses override as needed.

Important Points
  • Some important points to remember about method overriding in Java:
    1. Method overriding requires the subclass method to have the same name, return type, and parameter list as the superclass method.
    2. The overriding method must be in a subclass, not in the same class.
    3. The access modifier of the overriding method cannot be more restrictive than that of the overridden method.
      (For example, if the parent method is public, the overriding method cannot be private.)
      
      // Valid override
      protected void display() {}
      
      // Invalid override (if superclass method is public)
      private void display() {}
                      
    4. Static methods, constructors, and the main method cannot be overridden.
    5. Methods marked as final, static, or private cannot be overridden.
    6. The overriding method can throw only the same or narrower checked exceptions than the overridden method.
    7. The @Override annotation is recommended to avoid mistakes and improve code readability.
      
      @Override
      void methodName() {
          // implementation
      }
                      
    8. Method overriding is used to achieve runtime polymorphism or dynamic method dispatch.