In this tutorial, we are going to see Method overloading and overriding interview questions.
Table of Contents
- 1. What is method overloading?
- 2. What are rules of method overloading?
- 3. Can we overload static methods in java?
- 4. Can you overload main method?
- 5. Can we change only return type while method overloading?
- 6. What is method overriding?
- 7. What are rules of method overriding?
- 8. Â Can you override static methods in java?
- 9. Can you override private methods in java?
- 10. Can you override final methods?
- 11. What is static binding?
- 12. What is dynamic binding?
- 13. What are Covariant return type in java?
- 14. Predict output of below program:
- 15. Predict output of below program:
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?
4. Can you overload main method?
Answer:
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?
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?
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?
Answer:
Binding of overridden methods happen at runtime is known as dynamic binding.
13. What are Covariant return type in java?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package org.arpit.java2blog; public class BaseClass { public A m1() { System.out.println("In BaseClass method"); return new A(); } public static void main(String args[]) { BaseClass b=new SubClass(); b.m1(); } } class SubClass extends BaseClass { public B m1() { System.out.println("In SubClass method"); return new B(); } } class A { } class B extends A { } |
14. Predict output of below program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class MethodOverloadingExample { public void methodTest(Object object) { System.out.println("Calling object method"); } public void methodTest(String object) { System.out.println("Calling String method"); } public static void main(String args[]) { MethodOverloadingExample moe=new MethodOverloadingExample(); moe.methodTest(null); } } |
1 2 3 |
Calling String method |
When we have two overloaded version of same method, JVM will always call most specific method.
15. Predict output of below program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.io.IOException; public class MethodOverrdingTestMain { public static void main(String[] args) { B b=new B(); try { b.method(); } catch (Exception e) { e.printStackTrace(); } } } class A{ public void method() throws IOException { } } class B extends A{ public void method() throws Exception { } } |
1 2 3 |
compile time error |
You may also like:
- Core java interview questions
- Java Collections interview questions
- Java String interview questions
- OOPs interview questions in java
- Java Multithreading interview questions
- Exceptional handling interview questions in java
- Java Serialization interview questions in java
- web services interview questions
- restful web services interview questions
- Data structure and algorithm Interview Questions
- Spring interview questions
- Hibernate interview questions
good learning website