
Last week I wrote Java Method Hiding and Overriding: Override Static Method in Java here.
But I realized, it’s worth sharing some more information on Java Method Overriding
.
Rules for method overriding:
- In java, a method can only be written in Subclass, not in same class.
- The argument list should be exactly the same as that of the overridden method.
- The return type should be the same or a subtype of the return type declared in the original overridden method in the super class.
- The access level cannot be more restrictive than the overridden method’s access level.
- Instance methods can be overridden only if they are inherited by the subclass.
- A method declared final cannot be overridden.
- A method declared static cannot be overridden but can be re-declared.
- If a method cannot be inherited then it cannot be overridden.
- A subclass within the same package as the instance’s superclass can override any superclass method that is not declared private or final.
- A subclass in a different package can only override the non-final methods declared public or protected.
- 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.
- Constructors cannot be overridden.
Sample Example:
package com.crunchify.tutorials; public class CrunchifyObjectOverriding { public static void main(String args[]) { Company a = new Company(); // Company reference and object Company b = new eBay(); // Company reference but eBay object a.address();// runs the method in Company class b.address();// Runs the method in eBay class } } class Company { public void address() { System.out.println("This is Address of Crunchify Company..."); } } class eBay extends Company { public void address() { System.out.println("This is eBay's Address..."); } }
Result:
This is Address of Crunchify Company... This is eBay Rock's Address...
In the above example you can see that the even though b
is a type of Company
it runs the move method in the eBay
class.
Reason:
In compile time the check is made on the reference type. However in the runtime JVM figures out the object type and would run the method that belongs to that particular object.
Therefore in the above example, the program will compile properly since Company
class has the method move. Then at the runtime it runs the method specific for that object.
Using the super keyword:
When invoking a superclass version of an overridden method the super
keyword is used.
package com.crunchify.tutorials; public class CrunchifyObjectOverriding { public static void main(String args[]) { Company a = new Company(); // Company reference and object Company b = new eBay(); // Company reference but eBay object a.address();// runs the method in Company class b.address();// Runs the method in eBay class } } class Company { public void address() { System.out.println("This is Address of Crunchify Company..."); } } class eBay extends Company { public void address() { super.address(); // invokes the super class method System.out.println("This is eBay's Address..."); } }
Result:
This is Address of Crunchify Company... This is Address of Crunchify Company... This is eBay's Address...
You basically defined dynamic-polymorphism.
Yeah. Method is being invoked is not known at compile time but only available at run time.
Does address() is override from Company class or its scope is different?
It depends Sachin based on scenario. Please share some more details as I couldn’t get what you are referring.
Is it valid to access ebay class method from Company b = new eBay();
like b.getCompanyName();
Hi Sachin – Absolutely yes. If you have sample example in mind, please share and we will go over it.
Hello sir,
this is really very useful example of method overriding in java.
please keep posting for us
thank you so much
Thank you Anurag for kind words. Please let us know if you want to publish Java articles on Crunchify.
Good one.
Thanks. Can you tell me the font name? not in black console. font in first image with white background
Hi Anmol – it’s Monaco 11. Please find attached image from Eclipse.