Method overloading and overriding interview questions in java

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

1. What is method overloading?

Answer:
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:

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?

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

4. Can you overload main method?

Answer:
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?

Answer:
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?

Answer:
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?

Answer:
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?

Answer:
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?

Answer:
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?

Answer:
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?

Answer:
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:

Output:

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

15. Predict output of below program:

Output:

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

You may also like:

Related Posts

  • 22 May

    Java interview questions

    I have written a lot of java interview questions and this is index posts for all java interview questions. I will keep adding more interview questions to this post. Core Java Core java interview questions This list includes top 50 core java interview questions. Whether you are fresher or experienced programmer, this interview questions will […]

  • 09 November

    SOLID Principles in Java

    In this post, we will see 5 SOLID Principles in Java. Robert C. Martin gave five objected oriented design principles, and the acronym S.O.L.I.D is used for it. Each letter of this acronym talks about principles in Java. When you use all the principles of S.O.L.I.D in a combined manner, it becomes easier for you […]

  • 08 November

    Java interview questions for 2 years experience

    In this post, we are going to see Java interview questions for 2 to 3 years experienced. These are the most asked interview questions for freshers(0-3 years experienced). This question list will help you to crack java interview. I have already shared detailed answer over here before, you might find it helpful as well. I […]

  • 08 November

    Java interview questions for 5 years experience

    In this post, we will see Java interview questions for 5 to 6 years experience. When you have 5 years of experience as java developer, you need to have a good understanding of collections, multithreading concepts. If you are looking for the below queries then this post will help you as well. Java interview questions […]

  • 11 September

    Java technical interview questions

    In this post, we will see top Java technical interview asked in the Core Java interview.I have covered a lot of interview question on Java programming. Here is list of other java interview question you might find useful. Core java interview questions Java Collections interview questions Java String interview questions OOPs interview questions in java […]

  • 06 August

    Top 15 Spring Boot Interview questions

    In this post, we will see top 15 Spring Boot interview questions with answers.If you want to read more about Spring boot, you can go through Spring boot tutorial. If you are looking for below queries then this post will help you as well. Spring boot interview questions for 3 years experience Spring boot interview […]

Comments

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.