Wednesday, June 24, 2015

Java - Overriding method


Overriding Method :


Method in a subclass with the same signature (name,number and the type of its parameters) and return type as method in the super class overrides the super class's method.

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.

In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type.


Super keyword in Overriding


super keyword is used for calling the parent class method/constructor. super.methodname() calling the specified method of base class while super() calls the constructor of base class. Let’s see the use of super in Overriding



Static Methods


If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.

The distinction between hiding a static method and overriding an instance method has important implications:
  1. The version of the overridden instance method that gets invoked is the one in the subclass.
  2. The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.

Advantage of method overriding :

The main advantage of method overriding is that the class can give its own specific implementation to a inherited method without even modifying the parent class(base class).


Rules of method overriding in Java:

  1. Argument list: The argument list of overriding method must be same as that of the method in parent class. The data types of the arguments and their sequence should be maintained as it is in the overriding method.
  2. Access Modifier: The Access Modifier of the overriding method (method of subclass) cannot be more restrictive than the overridden method of parent class. For e.g. if the Access Modifier of base class method is public then the overriding method (child class method ) cannot have private, protected and default Access modifier as all of the three are more restrictive than public.
  3. Private, static and final methods cannot be overridden as they are local to the class. However static methods can be re-declared in the sub class, in this case the sub-class method would act differently and will have nothing to do with the same static method of parent class.
  4. Overriding method (method of child class) can throw any unchecked exceptions, regardless of whether the overridden method(method of parent class) throws any exception or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method.
  5. If a class is extending an abstract class or implementing an interface then it has to override all the abstract methods unless the class itself is a abstract class
Example of Overriding in Java:


public class OverrideBaseClass {

public int add (int a,int b){
int c= a+b;
return c;
}
}
public class OverridingChildClass extends OverrideBaseClass {

public int add (int a,int b){
System.out.println("Addition is " +super.add(5,6));
int c= a+b+5;
return c;
}
public static void main(String[] args) {

OverrideBaseClass overr = new OverridingChildClass();
System.out.println("Addition is " + overr.add(5,6));
}
}



Output: 
Addition is 11
Addition is 16

No comments:

Post a Comment