Method overloading and overriding interview questions in java

Method overloading and overriding interview questions

In this tutorial, we are going to see Method overloading and overriding interview questions.

1. What is method overloading?

If two or more methods have same name, but different argument then it is called method overloading.
For example:
Array’s sort method have many overloaded versions. You can sort array of double, int, String etc.

2. What are rules of method overloading?

Rules of Method overloading:

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️
Number of Arguments Overloaded method can have different number of arguments
Date type Overload method can have different data type for argument
Return type Return type can be changed but either number of argument or data type of argument should also be changed.
Order of arguments If you change sequence of arguments then it is also valid method overloading provided you have different data types arguments.
Constructor Can be overloaded

3. Can we overload static methods in java?

Yes, we can overload static methods in java but we can not override them.

4. Can you overload main method?

Yes, you can overload main method but only method with signature public static void main(String[] args) will be used when your class is invoked by JVM.

5. Can we change only return type while method overloading?

You can not.If we change only return type, it will become ambiguous for compiler to figure out which method to call.That is why you can not change only return type.

6. What is method overriding?

If subclass is having same method as base class then it is known as method overriding Or in another words, If subclass provides specific implementation to any method which is present in its one of parents classes then it is known as method overriding.

7. What are rules of method overriding?

Rules for method overriding:

Arguments Must not change
Return type Can’t change except for covariant (subtype) returns
Access Modifier Must not be more restrictive. Can be less restrictive.
Exceptions Can reduce or eliminate but must not throw new/broader checked exceptions
Contructor Can not be overridden
Static method Can not be overridden
final method Can not be overridden

8.  Can you override static methods in java?

No, you can not override static methods in java. Static methods belongs to class level not at object level.You can create static method with same name in child class and it won’t give you compilation error but it is called method hiding. You won’t get overriding behaviour with it.

9. Can you override private methods in java?

No, you can not override private methods in java. Private methods are not visible to child class, hence you can not override it , you can only hide it.

10. Can you override final methods?

Because final methods are meant to be not overridden.You declare a method final because you don’t want it to be overridden in subclass.

11. What is static binding?

When you compile Java program. During compilation process, compiler bind method call to actual method. This is called static binding and method overloading binding happens at compile time.

12. What is dynamic binding?

Binding of overridden methods happen at runtime is known as dynamic binding.

13. What are Covariant return type in java?

Covariant return type means if subclass overrides any method, return type of this overriding method can be subclass of return type of base class method.
For example:
Above example is perfect example of covariant return type.

14. Predict output of below program:

When we have two overloaded version of same method, JVM will always call most specific method.

15. Predict output of below program:

As exception thrown by overridden method can not be more restrictive, it will result in compile time error.

You may also like:

Related Posts


Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.