Basic interview questions about Java classes

Q. How many ways we can have method overloading in Java?

Answer: We can have method overloading in three different ways:
1. Same name with different number of parameters

   summation(int, int);
   summation(int, int, int);

2. Same name with different type of parameters

   summation(int, int);
   summation(int, float);

3. Same name with different order of parameters

   summation(int, int);
   summation(float, int);


Q. When method overloading throws compilation error?
Answer: When two methods have same name, same parameters but different return types. In this case Java will throw compilation error. 

   int summation(int, int);
   float summation(float, int);

Q. Does method overloading also known as static polymorphism? 
Answer: Yes, method overloading is an example of static polymorphism. It is also known as compile time binding or early binding.

Comments