Overloading vs Overriding in Java

Method overloading and overriding ( in other words, polymorphism in java) is neither a very difficult concept and nor it’s one of very unknown topics. Yet, I am bringing this topic here in this post, because at the same time it is very easy to make mistakes when such concepts are tested in java interviews using multiple code examples. I am not giving any new concept here, but I intend to revise your existing knowledge regarding rules of method overloading and overriding in java.

Method Overloading Rules

Here are the rules which you keep in mind while overloading any method in java:

1) First and important rule to overload a method in java is to change method signature. Method signature is made of number of arguments, type of arguments and order of arguments if they are of different types.

public class DemoClass {
	// Overloaded method
	public Integer sum(Integer a, Integer b) {
		return a + b;
	}

	// Overloading method
	public Integer sum(Float a, Integer b) {  //Valid
		return null;
	}
}

2) Return type of method is never part of method signature, so only changing the return type of method does not amount to method overloading.

public class DemoClass {
	// Overloaded method
	public Integer sum(Integer a, Integer b) {
		return a + b;
	}

	// Overloading method
	public Float sum(Integer a, Integer b) {     //Not valid; Compile time error
		return null;
	}
}

3) Thrown exceptions from methods are also not considered when overloading a method. So your overloaded method throws the same exception, a different exception or it simply does no throw any exception; no effect at all on method loading.

public class DemoClass {
	// Overloaded method
	public Integer sum(Integer a, Integer b) throws NullPointerException{
		return a + b;
	}

	// Overloading method
	public Integer sum(Integer a, Integer b) throws Exception{ 	//Not valid; Compile time error
		return null;
	}
}

Read More: What is polymorphism in java

Method Overriding Rules

We read above the rules for method overloading, now its time to list down the rules which you should keep remember while overriding a method in java.

1) The method argument list in overridden and overriding methods must be exactly same If they don’t match, you will end up with an overloaded method.

2) The return type of overriding method can be child class of return type declared in overridden method.

public class SuperClass {
	//Overriden method
	public Number sum(Integer a, Integer b) {
		return a + b;
	}
}

class SubClass extends SuperClass {
	//Overriding method
	@Override
	public Integer sum(Integer a, Integer b) {  	//Integer extends Number; so it's valid
		return a + b;
	}
}

3) Above all rules, private, static and final methods can not be overridden in java in any way. As simple as that !!

public class SuperClass {
	private Integer sum(Integer a, Integer b) {   //private method; overriding not possible
		return a + b;
	}
}

class SubClass extends SuperClass {
	//Overriding method
	public Integer sum(Integer a, Integer b) {   
		return a + b;
	}
}

4) Overriding method can not throw checked Exception higher in hierarchy than thrown by overridden method. Let’s say for example overridden method in parent class throws FileNotFoundException, the overriding method in child class can throw FileNotFoundException; but it is not allowed to throw IOException or Exception, because IOException or Exception are higher in hierarchy i.e. super classes of FileNotFoundException.

More to it, you can omit the exception declaration from overriding method. It’s allowed and perfectly valid. Also overriding method can throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception.

public class SuperClass {
	//Overriden method
	public Integer sum(Integer a, Integer b) throws FileNotFoundException {
		return a + b;
	}
}

class SubClass extends SuperClass {
	//Overriding method
	public Integer sum(Integer a, Integer b) throws IOException {   	//Not valid; Compile time error
		return a + b;
	}
	//Exception IOException is not compatible with throws clause in SuperClass.sum(Integer, Integer)
	public Integer sum(Integer a, Integer b)  {						//It's valid; Don't declare the exception at all is permitted.
		return a + b;
	}
}

5) Also note that overriding method can not reduce the access scope of overridden method. Put in simple words, if overridden method in parent class is protected, then overriding method in child class can not be private. It must be either protected (same access) or public (wider access).

public class SuperClass {
	//Overriden method
	protected Integer sum(Integer a, Integer b) {
		return a + b;
	}
}

class SubClass extends SuperClass {
	//Overriding method
	//Not valid; Compile time error "Cannot reduce the visibility of the inherited method from SuperClass"
	private Integer sum(Integer a, Integer b)  {	
		return a + b;
	}
}

Not to repeat again that method overriding is legal when talking in terms on parent classes and child classes. It does not happen within same class.

To verify that you are correctly overriding a method or not, simply use the annotation @Override on overriding method. It will verify all the method overriding rules for you. If there is any issue, it will result in compile time error.

Read More: Java Interview Questions

That’s all for this simple yet important concept to brush your basics in core java and object oriented programming.

Happy Learning !!

Ref: Oracle Blog

Leave a Reply

17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.