Method Overriding In Java

Introduction

 
In this article, we will discuss method overriding in JAVA.
 

What is Method Overriding?

 
In a class hierarchy, when a method in a sub class has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a sub-class, it will always refer to the versions of that method defined by the subclass.
 
Some Rules for method overriding
  1. The argument parameter should be exactly the same as that of the overridden method.
  2. A method declared static cannot be overridden but can be re-declared.
  3. The return type should be the same or a subtype of the return type declared in the original overridden method in the super class.
  4. A method declared final cannot be overridden.
  5. Constructors cannot be overridden.
  6. If a method cannot be inherited then it cannot be overridden.
  7. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.
  8. A subclass in a different package can only override the non-final methods declared public or protected.
  9. An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.
Method versions are hidden. Let's take an example.
 
Defining a class Ab which is to be override
  1. // method overriding  
  2. class Ab  
  3.   {  
  4.     int i,j;  
  5.     Ab(int a, int b)  
  6.       {  
  7.         i=a;  
  8.         j=b;  
  9.       }  
  10.     // display i and j  
  11.     void show()  
  12.       {  
  13.         System.out.println("i and j: " +i+" " + j );  
  14.       }  
  15.   }  
Defining another class B which inherit the previous one (class Ab) and define a class Override containing main method which shows the value of K as shown in fig.
  1. class B extends Abc  
  2.   {  
  3.     int k;  
  4.     B(int a, int b, int c)  
  5.       {  
  6.         super(a, b);  
  7.         k = c;  
  8.       }  
  9.     //display k-this overrides show() in A  
  10.     void show()  
  11.       {  
  12.         System.out.println("K: " + k);  
  13.       }  
  14.   }  
  15. class Override  
  16.   {  
  17.     public static void main(String[] args)  
  18.       {  
  19.         B subOb=new B(123);  
  20.         subOb.show();// this calls show() in B  
  21.       }  
  22.   }  
Output
overriding1.jpg
 
Important key concept to remember
 
Method overriding occurs only when the names and the arguments of the two methods are identical. If they are not similar, then the methods are simply overloaded.
 
For example
  1. // Method with different type signatures are overloaded-not   
  2. //overridden  
  3. class Ab  
  4.   {  
  5.     int i,j;  
  6.     Ab(int a, int b)  
  7.       {  
  8.         i=a;  
  9.         j=b;  
  10.       }  
  11.     // display i and j  
  12.     void show()  
  13.       {  
  14.         System.out.println("i and j: " +i+" " + j );  
  15.       }  
  16.   }  
Create other class B- in this class we extend class Ab and inherits its properties and we have to show how the class method get overloaded not overridden we define a main class Override as shown below
  1. class B extends Ab  
  2.   {  
  3.     int k;  
  4.     B(int a, int b, int c)  
  5.       {  
  6.         super(a, b);  
  7.         k = c;  
  8.       }  
  9.     //display k-this overrides show() in A  
  10.     void show()  
  11.       {  
  12.         System.out.println("K: " + k);  
  13.       }  
  14.   }  
  15. class Override  
  16.   {  
  17.     public static void main(String[] args)  
  18.       {  
  19.         B subOb=new B(123);  
  20.         subOb.show();// this calls show() in B  
  21.       }  
  22.   }  
Output
overriding3.jpg
 

Why Overridden Methods?

 
As stated earlier, overridden methods allow Java to support run-time polymorphism. Polymorphism is essential to object-oriented programming for one reason: it allows a general class to specify methods that will be common to all its derivatives, while allowing subclass to define the specific implementation of some or all of those methods. Overridden methods are another way that Java implements the "one interface, multiple methods" aspect of polymorphism.
 
Dynamic, run-time polymorphism is one of the most powerful mechanism that object-oriented design brings to bear on code reuse and robustness. The ability of exciting code libraries to call methods on instances of new classes without recompiling while maintaining a clean abstract interface is a profoundly powerful tool.
 
Applying Method Overriding
 
The below program creates a superclass class Figure, also define another method area() which is used to find the area.
 
Create a class Fig which define body structure of fig and passing two-variable val1 and val2, to find the area.
  1. // using run-time polymorphism.  
  2. class Fig  
  3.   {  
  4.     double val1;  
  5.     double val2;  
  6.     Fig(double a, double b)  
  7.       {  
  8.         val1=a;  
  9.         val2=b;  
  10.       }  
  11.     double area()  
  12.       {  
  13.         System.out.println("Area for figure is undefined.");  
  14.         return 0;  
  15.       }  
  16.   }  
Create another class Rect which contain Rectangle structure
  1. class Rect extends Fig  
  2.   {  
  3.     Rect(double a, double b)  
  4.       {  
  5.         super(a, b);  
  6.       }  
  7.     //override area for Rectangle  
  8.     double area()  
  9.       {  
  10.         System.out.println("Inside Area for Rectangle:");  
  11.         return val1*val2;  
  12.       }  
  13.   }  
Create another class Tri which contains triangle structure
  1. class Tri extends Fig  
  2.   {  
  3.     Tri(double a, double b)  
  4.       {  
  5.         super(a, b);  
  6.       }  
  7.     // override area for right triangle  
  8.     double area()  
  9.       {  
  10.         System.out.println("Inside area for Triangle.");  
  11.         return val1*val2/2;  
  12.       }  
  13.   }  
Create a main class FindAreas which is used to print the area of Rectangle, Triangle and Figure.
  1. class Findareas  
  2.   {  
  3.     public static void main(String args[])  
  4.       {  
  5.         Fig f=new Fig(1010);  
  6.         Rect r=new Rect(95);  
  7.         Tri t=new Tri(108);  
  8.         Fig figref;  
  9.         figref=r;  
  10.         System.out.println("Area is " + figref.area());  
  11.         figref=t;  
  12.         System.out.println("Area is " + figref.area());  
  13.         figref=f;  
  14.         System.out.println("Area is " + figref.area());  
  15.       }  
  16.   }  
Output
overridng2.jpg


Similar Articles