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:

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 in java 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:

Was this post helpful?

Related Posts

  • Hibernate interview questions
    16 April

    Top 40+ Hibernate interview questions and answers

    Table of Contents1. What is ORM?2. What is Hibernate?3. What are advantages of Hibernate?4. Explain architecture of Hibernate?5. What are some core interfaces of hibernate?6. Explain brief about Session interface used in hibernate?7. Are session objects thread safe?8. Explain brief about SessionFactory object used in hibernate?9. What is Configuration class in hibernate?10. Explain brief about […]

  • Java Interview questions
    22 May

    100+ Core Java Interview Questions and Answers for 2022

    Table of ContentsIntroductionJava Interview QuestionsJava Interview TipsMore Java Interview Q&ACore java interview questionsCollections interview questionsMultithreading interview questionsJava 8 interview questionsOOPs interview questionsException Handling interview questionsString interview questionsSerialization interview questionsmethod overloading and method overriding interview questionsImmutable class interview questionsJava tricky interview questions Basic interview questions for fresher Interview questions for 5 to 7 years experiencedHow HashMap […]

  • Solid principles in java
    09 November

    SOLID Principles in Java

    Table of ContentsWhat is the meaning of S.O.L.I.D?Single Responsibility Principle (SRP)Open Closed Principle Liskov Substitution Principle Interface Segregation PrincipleDependency Inversion PrincipleWrap Up 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 […]

  • Java interview questions for 2 years experience
    08 November

    Java interview questions for 2 years experience

    Table of Contents1.   Can we override static method in java?2.   Can you overload main method in java?3. Can we override private methods in java?4. What is the base class for all the classes?5.   Can you list down some of important method from object class?6.   Which two methods should you override while putting the custom object as […]

  • Java interview questions for 5 years experience
    08 November

    Java interview questions for 5 years experience

    Table of Contents1. Guess the output of below program.2. Guess the output of below program.3. Guess the output of below program.4. What will happen in case of below program?5. What will happen in case of below program?6. What will happen in case of below program?7. Guess the output of below program.8. Guess the output of […]

  • Java tricky interview questions
    11 September

    Java technical interview questions

    Table of Contents1 How HashMap works in java?2 Which methods you should implement for key Object in HashMap?3. Guess the output of below program.4. Guess the output of below code.5. What are differences between Comparator and Comparable?6. What is difference between Error and Exception?7. What is ClassLoader in java?8. What are types of ClassLoader in java?9. […]


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.