Rules for method Overriding in Java

Overriding is very significant and confusing part of Java language. Sometime experienced developers get confused on Overridden rules. In this blog you can understand these rules with good and simple illustrations. Overridden rules in Java are given below:

1. Arguments: Arguments must not change during overriding any method. Even you can not use subtype of any class as argument in case of overridden method. You must have use exact same argument type used in Super method. For example:

class SuperClass {

	public void method(SuperClass args) {
		System.out.println(args);
	}

}

class SubClass extends SuperClass {

	//Invalid Overridden method, because argument type must be same.
	@Override
	public void method(SubClass args){
		System.out.println(args);
	}

	//Valid Overridden method, because argument is same here.
	@Override
	public void method(SuperClass args){
		System.out.println(args);
	}
}

2. Return types: Return type of overridden method can not change except subtype or sub-class of return type. This was not valid before Java5. But now it is valid since Java5. For example:

class SuperClass {

	public SuperClass method() {
		return null;
	}

}

class SubClass extends SuperClass {

	// Valid Overridden method, because return type is same as in overridden method,	
	@Override
	public SuperClass method() {
		return null;
	}

	// Valid Overridden method, because return type is sub-class of overridden method return type
	@Override
	public SubClass method() {
		return null;
	}

	// Invalid Overridden method, because return type is neither same type of overridden method return type nor sub-class of Overridden method return type.
	@Override
	public String method() {
		return null;
	}
}

3. Access Modifier: Access modifier of overridden method can not be more restrictive. But it can be less restrictive. Default access modifier can be Protected or Public and Protected access modifier can be Public. But Public can not be Protected or Default. For example:

class SuperClass {

	protected void method() {

	}

}

class SubClass extends SuperClass {

	// Valid Overridden method, because Protected modifier can be Public.
	@Override
	public void method() {

	}

	// Invalid Overridden method, because Protected modifier can not be Default.
	@Override
	void method() {

	}
}

4. Method Invocation: Which method would be called is depends on object type at runtime. For example:

class SuperClass {

	public void method() {
		System.out.println("SuperClass method called!");
	}

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

}

class SubClass extends SuperClass {

	@Override
	public void method() { // Valid Overridden method
		System.out.println("SubClass method called!");
	}

}

Output of this program is “SubClass method called!” because at runtime Object of SubClass was created by SubClass constructor.

5. Exceptions: Rules related to exceptions during method overriding is quite complicated. So you need to understand it more carefully. Most important points are given below:

• Exception can reduce during override.
• Exception can eliminate also.
• Must not throw new checked exception.
• Must not throw new broader checked exception.
• New RuntimeException can be thrown and can be eliminate.

For example:

class SuperException extends Exception {};
class SubException extends SuperException {};

class AnyException extends Exception {};
class OneMoreException extends Exception {};

class NewRuntimeException extends RuntimeException {};

class SuperClass {

	public void method() throws SuperException, AnyException {
		System.out.println("Hello!");
	}
}

class Reduced extends SuperClass {

	// Valid Overriding
	@Override
	public void method() throws SuperException { // Reduced
		System.out.println("Hello!");
	}
}

class Eliminated extends SuperClass {

	// Valid Overriding
	@Override
	public void method() { // Reduced
		System.out.println("Hello!");
	}
}

class Narrow extends SuperClass {

	// Valid Overriding
	@Override
	public void method() throws SubException { // Narrow
		System.out.println("Hello!");
	}
}

class NewException extends SuperClass {

	// Invalid Overriding, Compiler error
	@Override
	public void method() throws OneMoreException { // New Checked Exception
		System.out.println("Hello!");
	}
}

class Broader extends SuperClass {

	// Invalid Overriding, Compiler error
	@Override
	public void method() throws Exception { // New Broader Checked Exception
		System.out.println("Hello!");
	}
}

class NewRuntime extends SuperClass {

	// Valid Overriding
	@Override
	public void method() throws NewRuntimeException { // New RuntimeException
		System.out.println("Hello!");
	}
}

This is all, how method overriding works in Java

8 thoughts on “Rules for method Overriding in Java

  1. My spouse and I absolutely love your blog and find a lot of your post’s to be what precisely I’m looking for. Do you offer guest writers to write content available for you? I wouldn’t mind publishing a post or elaborating on a few of the subjects you write with regards to here. Again, awesome blog!

  2. Hi! This is my first visit to your blog! We are a group of volunteers and starting a new project in a community in the same niche. Your blog provided us valuable information to work on. You have done a marvellous job!

  3. Pretty nice post. I simply stumbled upon your weblog and wished to say that I’ve truly enjoyed surfing around your blog posts. After all I’ll be subscribing in your rss feed and I’m hoping you write again very soon!

  4. Hello! Khajan,

    Method call in method overriding is totally depends on object reference on which method called. According to your query, you have initialized object of SubClass, therefore SubClass method will be called in this case. Hope I have addressed your query.

    Thanks

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>