Categories: Java

Method Overriding in Java

  • In Java, method overriding occurs when a subclass (child class) has the same method as the parent class.
  • Overriding is a feature that allows a subclass or child class to implement a method that is already provided by one of its super-classes or parent classes.
  • When a subclass’s method has the same name, parameters, signature, and return type (or subtype) as a method in its superclass, the subclass’s method is said to override the superclass’s method.
  • Method overriding is one of the ways by which java achieves Run Time Polymorphism.
Application of Java Method Overriding:
  • Method overriding is a technique for providing a custom implementation of a method that is already provided by its superclass.
  • For runtime polymorphism, method overriding is used.
Java Method Overriding Rules:
  • The method’s name must match that of the parent class.
  • The parameter must be the same as in the parent class.
  • An IS-A relationship is required (inheritance).
Method Overriding Example:
class Bank{  
int getRateOfInterest()
  {
    return 0; 
      
  }  
}  
//Creating child classes.  
class SBI extends Bank{  
int getRateOfInterest() 
  {
      return 8;
      
  }  
}  
  
class ICICI extends Bank{  
   int getRateOfInterest()
   {
       return 7;
       
   }  
}  
class AXIS extends Bank{  
   int getRateOfInterest()
   {
       return 9;
       
   }  
}  
//Test class to create objects and call the methods  
class Test2{  
   public static void main(String args[])
   {  
   SBI s=new SBI();  
   ICICI i=new ICICI();  
   AXIS a=new AXIS();  
   System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());  
   System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());  
   System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());  
     }  
}  
Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
Points to remember:
  • The super keyword can be used to invoke the parent class method in an overriding method.
  • We can’t override the constructor because parent and child classes can’t have the same constructor name (The constructor name must always be the same as the Class name).
  • Overriding a static method is not possible. Runtime polymorphism can prove it, so we’ll learn it later.
  • A static method is bound to a class, whereas an instance method is bound to an object, therefore a static method cannot be overridden.
  • Because main is a static method, it cannot be overridden.

Note: also read about the Method Overloading

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago