Method Overloading and Overriding

JAVA

Method Overloading and Overriding Interview Questions

  1. What is the other name of Method Overloading?
  2. How will you implement method overloading in Java?
  3. What kinds of argument variations are allowed in Method Overloading?
  4. Why it is not possible to do method overloading by changing the return type of method in java?
  5. Is it allowed to overload the main() method in Java?
  6. How do we implement method overriding in Java?
  7. Are we allowed to override a static method in Java?
  8. Why Java does not allow overriding a static method?
  9. Is it allowed to override an overloaded method?
  10. What is the difference between method overloading and method overriding in Java?
  11. Does Java allow virtual functions?
  12. What is meant by covariant return type in Java?

ANSWERS / HINTS

1- Method Overloading is also known as Static Polymorphism.
2- In Java, a class can have multiple methods with the same name but different arguments. It is called Method Overloading. To implement method overloading we have to create two methods with the same name in a class and do one/more of the following:
  • Different number of parameters
  • The different data type of parameters
  • A unique sequence of the data type of parameters
3-Method Overloading allows two methods with the same name to differ in:
  • Number of parameters
  • Data type of parameters
  • The sequence of the data type of parameters
4- If we change the return type of overloaded methods then it will lead to ambiguous behaviour. How will clients know which method will return what type? Due to these different return types are not allowed in overloaded methods.
5- Yes, Java allows users to create many methods with same name ‘main’. But only public static void main(String[] args) method is used for execution.
6- To override a method, we just provide a new implementation of a method with the same name in a subclass. So there will be at least two implementations of the method with the same name. One implementation is in a parent class. And another implementation is in child class.
7- No. Java does not allow overriding a static method. If you create a static method with the same name in subclass, then it is a new method, not an overridden method.
8- To override a method, you need an instance of a class. A static method is not associated with any instance of the class. So the concept of overriding does not apply here.
9- Yes. You can override an overloaded method in Java.
10- Differences between method overloading and overriding are:
  • Overloading is static polymorphism.
  • Overriding is runtime polymorphism.
  • Overloading occurs within the same class.
  • Overriding happens in two classes with the hierarchy relationship.
  • Parameters must be different in method overloading. Parameters must be the same in method overriding.
  • Method overloading is a compile-time concept. Method overriding is a runtime concept.

11- Yes. All instance methods in Java are virtual functions by default. Only class methods and private instance methods are not virtual methods in Java.

12- A covariant return type of a method is one that can be replaced by a “narrower” type when the method is overridden in a subclass.

Let say class B is child of class A. There is a get() method in class A as well as class B. get() method of class A can return an instance of A, and get() method of class B return an instance of B.  Here class B overrides get() method, but the return type is different.

Before Java 5, any method that overrides the method of parent class would have the same return type. From Java 5 onwards, a child class can override a method of parent class and the child class method can return an object that is the child of object return by the parent class method.

Check Interview Questions on Java

Wikipedia Java

1 thought on “Method Overloading and Overriding”

Leave a Comment

Your email address will not be published.