Java Method Override

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

public class ParentClass
{
public void show()
{
System.out.println("Show method of Super class");
}
}

public class SubClass extends ParentClass
{
//below method is overriding the ParentClass version of show method
public void show()
{
System.out.println("Show method of Sub class");
}
}

 

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.

public class SubClass extends superclass {

	void method() {
		super.method();
		System.out.println("In Sub Class");
	}

	public static void main(String[] args) {
		SubClass obj = new SubClass();
		obj.method();
	}
}

class superclass {
	void method() {
		System.out.println("In Super Class");
	}
}

Output

In Super Class
In Sub Class

 

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.

public class SubClass extends superclass {

	static void method() {
		// super.method(); // Super keyword will not work here. As it is not overriden method
		System.out.println("In Sub Class");
	}

	@SuppressWarnings("static-access") // The static method method() from the type SubClass should be accessed in a static way
	public static void main(String[] args) {
		SubClass obj = new SubClass();
		obj.method();
                SubClass.method();// It is same as above. Same method will be invoked
	}
}

class superclass {
	static void method() {
		System.out.println("In Super Class");
	}
}

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 >>
By | 2012-04-21T05:20:52+00:00 April 21st, 2012|Core Java|9 Comments

About the Author:

9 Comments

  1. Ankan April 23, 2013 at 7:33 am - Reply

    “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…

    • admin April 23, 2013 at 5:18 pm - Reply

      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

      • Deepika September 17, 2014 at 7:30 am - Reply

        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…

  2. manoj November 7, 2013 at 8:12 am - Reply

    If method can be overriden then its access modifier is changable as well.

  3. sam October 7, 2014 at 9:49 am - Reply

    nothing is helpfull in this

  4. Ravinder December 15, 2014 at 10:54 am - Reply

    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

  5. priyanka February 14, 2015 at 7:07 am - Reply

    i just love this site

  6. Yamini Sukhija June 15, 2015 at 12:13 pm - Reply

    Please explain dynamic method binding enabling runtime polymorphism

    • V Gautam June 15, 2015 at 4:33 pm - Reply

      I think you should be able to understand it now after clarification on reference and object.

Leave A Comment