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

Most important and Tricky Notes of Overload and Override method in Java


Most important and Tricky Notes of Overload and Override method in Java:

·        If we change the arguments of the overriding method, then that method will be treated as overloaded not overridden.

·        We can increase the visibility of overriding methods but can’t reduce it that is we can override the protected method of the subclass as a public method in the subclass
·         We can change the return type of overriding method from Number type to Integer type.
·        We can override a superclass method without throws clause as a method with throws a clause in the subclass but only with the unchecked type of exceptions.

·        We can change the exception of a method with throws clause from SQLException to NumberFormatException or any unchecked type of exceptions while overriding it.
·        We cannot change the exception of a method with throws clause from unchecked to checked while overriding it.
·        Using the super keyword, we can refer superclass a version of the overridden method in the subclass.

·        No question of overriding private methods. They are not at all inherited to subclass.

·        We can remove throws a clause of a method while overriding it.

·        We can’t override non-static methods as static.

·        We can change the exception of a method with throws clause from checked to unchecked while overriding it but the reverse is not possible.

·        We can change the number of exceptions thrown by the method with throws clause while overriding But, exceptions must be compatible with throws clause in the superclass method.

·        When you declare two static methods with the same name and signature in both superclass 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.
·        We cannot override a final method in Java; final keyword with the method is to prevent method overriding. You use the 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.

·        We cannot change the argument list of an overriding method. The argument list is part of the method signature and both overriding and overridden method must have the same signature.
·        The overloaded method in Java doesn't have such restriction and you are free to modify throws clause as per your need.
·        We 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:

No comments:

Post a Comment

Popular Posts

Recent Posts