Hey Guys, I am creating this blog to share deep knowledge in Java, JSP, Servlets, JDBC, Hibernate, Spring and Spring MVC in details.

Overload and Override Related Tricky Question for MNC


Overload and Override Related Tricky Question for MNC


1. What happens if we change the arguments of the overriding method?
If we change the arguments of the overriding method, then that method will be treated as overloaded not overridden.

2. Can we override the protected method of the superclass as a public method in the sub class?
Yes. You can increase the visibility of overriding methods but can’t reduce it.

3. Can we change the return type of overriding method from Number type to Integer type?
Yes. You can change as Integer is a subclass of Number type.

4. Can we override a superclass method without throws clause as a method with throws a clause in the subclass?
Yes, but only with the unchecked type of exceptions.

5. Can we change the exception of a method with throws clause from SQLException to NumberFormatException while overriding it?
Yes. The overridden method may throw SQLException or its subclass exception or any unchecked type of exceptions.

6. Can we change the exception of a method with throws clause from unchecked to checked while overriding it?
No. We can’t change the exception of a method with throws clause from unchecked to checked.

7. How do you refer superclass version of the overridden method in the subclass?
Using the super keyword, we can refer superclass a version of the overridden method in the subclass.

8. Can we override private methods?
No question of overriding private methods. They are not at all inherited to subclass.

9. Can we remove throws clause of a method while overriding it?
Yes. You can remove throws clause of a method while overriding it.

10. Is it possible to override non-static methods as static?
No. You can’t override non-static methods as static.

11. Can we change an exception of a method with throws clause from checked to unchecked while overriding it?
Yes. We can change an exception from checked to unchecked but the reverse is not possible.

12. Can we change the number of exceptions thrown by a method with throws clause while overriding it?
Yes, we can change. But, exceptions must be compatible with throws clause in the superclass method.

13. What is method hiding in Java? 
When you declare two static methods with the same name and signature in both super class and subclass then they hide each other i.e. a call to the method in the subclass will call the static method declared in that class and a call to the same method is super class is resolved to the static method declared in the superclass.



14. Can we override the final method in Java?
No, you cannot override a final method in Java; final keyword with the method is to prevent method overriding. You use final when you don't want subclass changing the logic of your method by overriding it due to security reason. This is why the String class is final in Java. This concept is also used in the template design pattern where the template method is made final to prevent overriding.

15. Can we change the argument list of an overriding method?
No, you cannot. The argument list is part of the method signature and both overriding and overridden method must have the same signature.


16. Can we overload a method which throws runtime exception without throws clause?
The overloaded method in Java doesn't have such restriction and you are free to modify throws clause as per your need.

17. How do you call superclass version of an overriding method in a subclass?
You can call a superclass version of an overriding method in the subclass by using super keyword. For example to call the toString() method from Object class you can call super.toString().


Share:

Related Posts:

No comments:

Post a Comment

Popular Posts