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:

Is it possible to override a method by changing the return type or what is covariant method overriding in Java?


Is it possible to override a method by changing the return type or what is covariant method overriding in Java?

Before JDK 5.0, it was not possible to override a method by changing the return type. When we override a parent class method, the name, argument types and return type of the overriding method in child class has to be exactly the same as that of the parent class method. The overriding method was said to be invariant with respect to return type.
Java 5.0 onwards it is possible to have different the return type for an overriding method in the child class, but the child’s return type should be sub-type of parent’s return type. The overriding method becomes variant with respect to return type.
Below is a simple example to understand the co-variant return type with method overriding.

class A {
}

class B extends A {
}
class Base {
     A fun() {
          System.out.println("Base fun()");
          return new A();
     }
}
class Derived extends Base {
     B fun() {
          System.out.println("Derived fun()");
          return new B();
     }
}
public class Main {
     public static void main(String args[]) {
          Base base = new Base();
          base.fun();

          Derived derived = new Derived();
          derived.fun();
     }
}
Output:
Base fun()
Derived fun()

Note: If we swap return types of Base and Derived, then above program would not work.
Advantages:
·        It helps to avoid confusing type casts present in the class hierarchy and thus making the code readable, usable and maintainable.
·        We get liberty to have more specific return types when overriding methods.
·        Help in preventing run-time ClassCastExceptions on returns

Share:

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:

Explaination of method Overloading and Type Promotion in Java (Very importent for MNC tricky question)


Explaination of method Overloading and Type Promotion in Java (Very importent for MNC tricky question)
Boolean---->Byte ---->Char ---->Short ---->int ---->long ---->float ---->double

Type promotion is an automatic type conversion from a "lesser" base type to a "greater" one. Let us see the diagram given below and understand it.

It must be clear from the above diagram that byte can be promoted to short, int, long, float or double. The short data type can be promoted to int, long, float or double. The char data type can be promoted to int, long, float or double and so on.
One type is promoted to its greater type implicitly it no matching data type is found.

 a)  Example of method overloading with Type Promotion:

public class TypePromotion {
     void add(float a, float b) {
          System.out.println(a + b + " float");
     }
     void add(long a, long b) {
          System.out.println(a + b + " long");
     }
     public static void main(String args[]) {
          TypePromotion obj = new TypePromotion();
          obj.add(21, 21); // integer value is passed
     }
}

Output: 42 long
In the above example, integer value is passed through obj.add(21,21) which will be implicitly promoted to its nearest bigger type i.e. long type.
b) Example of method overloading with Type Promotion:

class TypePromotion {
     void add(float a, float b) {
          System.out.println(a + b + " float");
     }
     void add(double a, double b) {
          System.out.println(a + b + " double");
     }
     public static void main(String args[]) {
          TypePromotion obj = new TypePromotion();
          obj.add(21, 21); // integer value is passed
     }
}

Output: 42.0 float

In the above example, an integer value is passed through obj.add(21,21) which will be implicitly promoted to its nearest bigger type i.e. float type. [Note: nearest bigger type of int is long which is not matched in the above program, therefore next bigger type is matched and is promoted to float implicitly]

c) Example of method overloading with Type Promotion

public class OverloadingExm {
     void sum(int a, int b) {
          System.out.println("int arg method invoked");
     }
     void sum(long a, long b) {
          System.out.println("long arg method invoked");
     }
     public static void main(String args[]) {
          OverloadingExm obj = new OverloadingExm ();
obj.sum(20, 20);// now int arg sum() method gets invoked
     }
}

Output: int arg method invoked

d) Example of method overloading with Type Promotion giving ambiguity

class TypePromotion {
     void add(int a, long b) {
          System.out.println(" 1st method invoked");
     }
     void add(long a, int b) {
          System.out.println(" 2nd method invoked");
     }
     public static void main(String args[]) {
          TypePromotion obj = new TypePromotion();
          obj.add(21, 21);
     }
}

Compile time error will be generated because one-type cannot be demoted to any type implicitly.

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method add(int, long) is ambiguous for the type TypePromotion at com.pcpl.ccl.TypePromotion.main(TypePromotion.java:12)
Share:

Popular Posts

Recent Posts