Java Method Override

This entry is part 20 of 34 in the series Core Java Course

Class inheriting the method from its super class has the option to override it. Benefit of overriding is the ability to define behavior specific to particular class. In case of concrete subclass it is forced to implement all methods defined in abstract class if no other super class implemented it in hierarchy. Overriding some times referred as Run time Binding. It means which overridden method is to be invoked will be determined by reference type and not the instance type.


 

Method Overriding Example


 

Method Override Rules

  • Overriding method can not have more restrictive access modifier than the method being overridden but it can be less.
  • The argument list must exactly match that of the overridden method, if they don’t it is more likely that you are overloading the method.
  • Return type must be the same as, or subtype of the return type declared in overridden method in Super class.
  • Overriding method can throw any unchecked exception(Runtime) but it can throw checked exception which is broader or new than those declared by the overridden method but it can not throw fewer or narrow checked exception.
  • Final method can not be overridden.
  • Static methods can not be overridden. Static method looks to overridden but it is hidden.
  • If a method cannot be inherited then it cannot be overridden.

Invoke Overridden method from Super class

What if you want to invoke super class overridden method before executing the subclass method. You can use SUPER keyword.

Output

 

Static Method can not be overridden

Static method can not be overridden. It might looks like it is overridden but it is not. Static method can be hidden.

Here super keyword can not be used to invoke super class method. As it is not overridden method from super class.

Cheat-sheet

  • Constructor can not be overriden.
  • Overriding methods must have the same argument set.
  • Overriden methods must have same return type. These return type can also be the subclass(covariant return).
  • Overriden method can not have more restrictive access modifier.
  • Overriden method can not throw new or broader exception(Checked).
  • Overriden method can throw any unchecked exception.
  • Final methods can not be overriden.
  • Private methods are not inherited to subclass hence it can not be overriden in subclass.
  • Polymorphism applies to overriding.
  • Object type determines which overridden method will be invoked and that will be decided at the runtime.

 

Series Navigation<< Overloading in javaJava Exceptions Tutorial >>

9 Comments Java Method Override

  1. Ankan

    “Overriding method can throw any unchecked exception(Runtime) but it can throw checked exception which is broader or new than those declared by the overriden method but it can throw fewer or narrow checked exception.”

    Did not get this point…

    Reply
    1. admin

      Hi Ankan,

      I got your message. Thanks for your kind words.
      As for as overloading and Overriding is concened, First you have to understand the concept of Exception.
      Exception can be divided two types.
      1- Checked
      2- Un-Checked

      Chcked exception is something which needs to be handled in code by developer while unchecked exception is not supposed to be handled in code, means developer is not forced to handle it in code.

      Now Broader exception.
      Exception has a particular hierarchy. When i say that broader means exception @ higher level in hierarchy.

      A is parent of B and B is parent of C.
      So if a method throws Exception of type C and overloaded/overriden method is throwing exception of type B then it is called broader exception throwing by overloaded or overriden method.

      For more details you can refer exception chapter.
      Hope it help you. Let me know in case you have more question.

      Thanks

      Reply
      1. Deepika

        Hello,

        Thanks for clarifying.

        I just wanted to summarize, what you have explained. Please correct m if I am wrong.

        If Parent class method is not throwing any exception,then child class overridden method can only throw unchecked(Run time) exception.(Not checked exception)

        If Parent class method is throwing any exception,then child class overridden method can only throw either same exception thrown by parent class method or no exception.But it should not throw any exception which is the child of exception thrown by parent class method.

        Thanks…

        Reply
  2. Ravinder

    Hi Bro!,
    I am not getting properly what you explain so According to your written rules(about Override concept) If you provide with example then we are appreciate to you …

    Thanks
    Adv
    ravinder

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *