Tuesday, 30 August 2016

Method Overriding Rules In Java

Method overriding rules Java


Parameters that need to consider in method overriding is,

What is Method Overriding?

In case of Inheritance, where Parent-child relationship exist, 
Parent class define a method say connect() which does connection to some remote service. 
Child class has better way to do connection than Parent class and don't want to use Parent class connect() method, So it overrides Parent's class connect() method by providing own implementation of connect() method, 

Now whenever connect() method is called using Child class object, then connect() method of Child class will be called and not the connect() method of Parent/Super class, this concept is called Method overriding.

Example of Method Overriding: 
class Parent{
 public void connect(){  
  System.out.println("Doing connection in Parent way");  
 }
}

class Child extends Parent {
 @Override
 public void connect() { 
  System.out.println("Doing connection in Child way");
 }
}

Rules of Method Overriding


Parameters that need to be consider in case of Method Overriding,
  1. Access Specifier of method
  2. Return Type of method
  3. Name of method
  4. Arguments/Parameters of method
  5. Exception that method throws. 

Access Specifier of method
Overriding method in Child class can either have same access specifier as that of Parent class method or can increase visibility but cannot decrease it.

If Parent class method is declared as,
protected void connect(){}
then valid access specifier for Child class overriding method connect() is, 
public void connect(){} OR
protected void connect(){}


Return Type of method:
Overriding method in Child class can either have same return type or should be Subclass of return type declared in method of Super class.

If Parent class method is declared as,
protected Number connect(){}

then valid Return type for overriding connect() method in Child class is either Number class or all subclass of Number class, 
public Integer connect(){}
protected Long connect(){}
protected Number connect(){}

Name of method:
Name of the overriding method in Child class must be exactly same as that of method declared in Parent Class


Arguments/Parameters of method: 
Total number and Type of arguments present in overriding Child class method must be exactly same as that of Parent class method.

Note:
Arguments present in Child class method should be exactly of same type (Subclass will not work) as that of Parent class method.
Why Subclass argument won't work, Let's understand it with below example,
class A1{}
class A2 extends A1{}
class A3 extends A1{}

class Parent{
 protected Number connect(A1 a1){  
  System.out.println("Doing connection in Parent way"); 
  return null;
 }
}

class Child extends Parent {
 @Override
 public Long connect(A2 a2) { 
  System.out.println("Doing connection in Child way");
  return null;
 }
}
In above example,
Super class connect method take generic agument A1.
Child class overriding connect method take specific agument A2

If Covariant parameters are allowed in Method overriding than what will be output of below lines,
Parent p = new Child();
p.connect(new A3());

Above line will call connect() method of Child class method due to Polymorphic behaviour. Also, According to Parent class method definition it should work but according to Child class overriding definition it will not work.

This problem occurred because Child class overriding method accepted specific(Subclass) argument compared to Parent class method which is accepting generic argument.

To avoid this situation, parameter in overriding method must be exactly same. 


Exception that method throws: 

Unchecked Exception:  
Overriding method in Child class can throw any number of Unchecked Exception irrespective of Parent class overriden method has declared any Checked/Unchecked Exception or not.  
Below example shows valid overriden method connect() and connect1().
class Parent{
 protected Object connect(String s1) {  
  System.out.println("Doing connection in Parent way"); 
  return null;
 }

 protected Object connect1(String s1) throws NullPointerException{  
  System.out.println("Doing connection in Parent way"); 
  return null;
 }
}

class Child extends Parent {
 @Override
 public Integer connect(String s2) throws RuntimeException, NullPointerException{ 
  System.out.println("Doing connection in Child way");
  return null;
 }

 @Override
 protected Object connect1(String s1) throws RuntimeException{  
  System.out.println("Doing connection in Parent way"); 
  return null;
 }

 public static void main(String[] args) {
  Parent p = new Child();
  p.connect("hello");
 }
}


Checked Exception:  
If say Overriden method of Parent class throws IOException, then overriding method in Child class can either throw 
  1. No Exception,
  2. Same IOException,
  3. Any number of Subclass of IOException like FileNotFoundException, EOFException etc.


Not Allowed:
If say Overriden method of Parent class throws IOException, then overriding method in Child class cannot throw
  1. It cannot throw exception from totally new inheritance hierarchy like SQLException.
  2. It cannot throw broader exception like Throwable or Exception in our case. 

Conclusion: 
Overriding method of Child class can throw any number of Unchecked Exception irrespective of Overriden method of Super class throwing or not throwing any exception.

If Super class overriden method throws Cheked Exception then Overriding method of Child class can either choose not to throw any exception, or throws same exception or throws any number of subclass of Exception thrown by overriden method

Lets see few example and understand:
class Parent{
 protected Object connect(String s1) {  
  System.out.println("Doing connection in Parent way"); 
  return null;
 }
 
 protected Object connect1(String s1) throws Exception{  
  System.out.println("Doing connection in Parent way"); 
  return null;
 }

 protected Object connect2(String s1) throws IOException, SQLException{  
  System.out.println("Doing connection in Parent way"); 
  return null;
 }
 
 protected Object connect3(String s1) throws IOException{  
  System.out.println("Doing connection in Parent way"); 
  return null;
 }

 protected Object connect4(String s1){  
  System.out.println("Doing connection in Parent way"); 
  return null;
 }

}

class Child extends Parent {
 @Override
 public Integer connect(String s2){ //It will work
  System.out.println("Doing connection in Child way");
  return null;
 }

 protected Object connect1(String s1) throws Exception, FileNotFoundException, MalformedURLException{ //It will work  
  System.out.println("Doing connection in Child way"); 
  return null;
 }
 
 protected Object connect2(String s1) throws FileNotFoundException{ //It will work
  System.out.println("Doing connection in Child way"); 
  return null;
 }
 
 protected Object connect3(String s1) throws Exception{ //It will NOT work  
  System.out.println("Doing connection in Child way"); 
  return null;
 }
 
 protected Object connect4(String s1) throws Exception{ //It will NOT work  
  System.out.println("Doing connection in Child way"); 
  return null;
 }
 
 public static void main(String[] args) {
  Parent p = new Child();
  p.connect("hello");
 }
}


In case if you like to try program using different Exception hierarchy, here is Exception hierarchy tree.


You may also like to see


Advanced Java Multithreading Interview Question-Answer.

How is ambiguous overloaded method call resolved in java?

Exception Handling Interview Question-Answer

Method Overloading - Method Hiding Interview Question-Answer

Type Casting Interview Questions In Java

Interface interview questions and answers in Java

Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.

No comments:

Post a Comment