Friday, 26 September 2014

Overriding in java

Meaning of overriding :


Overriding concept is associated with Inheritance . If we have 2 classes ,one extending the other then child class can override methods of parent class . But there are some rules which needs to be followed :

So we will see which are all these rules :

Rule 1 : The signature of method in the child class should be exactly same as that in Parent class which includes return type, method parameters and name.

Rule 2:Argument list should be exactly same as that of overridden method.

Rule 3: Constructors cannot be overridden .

Rule 4: Static methods cannot be overridden but can be re declared which is actually method hiding .This can be read in detail from

Overriding of static method in Java.


Rule 5 : Final methods cannot be overridden.

Imp things :

1. Overridden methods are bound at run time only.



This i will explain u with an example:


package test;

import java.util.ArrayList;
import java.util.List;

 class Parent
{
public void disp(){
System.out.println("in parent");
}
}


public class child extends Parent{
public void disp(){
System.out.println("in child");
}
public static void main(String[] args) {
child c= new child();
p.disp();
}
}

Output : in child 


method disp is being overridden in the above example 

Imp Thing : If you dont provide same arguments in method name of return type then there will be a compilation error.

Now as you can see we have made an object of child class with child class reference only . But what happens if we have child class reference and parent class object .


 Child class reference and parent class object in Overriding:

This thing can be understood with the help of an example :

package test;

import java.util.ArrayList;
import java.util.List;

 class Parent
{
public void disp(){
System.out.println("in parent");
}
}


public class child extends Parent{
public void disp(){
System.out.println("in child");
}
public static void main(String[] args) {
child c= new Parent();
c.disp();
//testing tst= new Parent();
}
}

In this case there will be a compilation error on the error which is shown as bold in the above example . But there is a solution that you can add casting of object to child  but that also lead to an exception that parent class object cannot be cast to child class.


Exception in thread "main" java.lang.ClassCastException: test.Parent cannot be cast to test.child
at test.child.main(child.java:20)

Now one more case where we have parent class reference and child class object .So what you thick in this case ?
Compilation error ?
No error?
or something else ?

ok let me tell you this case in detail also 

Parent class reference and child class object in Overriding:

Well this case can also seen with an example :
package test;

import java.util.ArrayList;
import java.util.List;

 class Parent
{
public void disp(){
System.out.println("in parent");
}
}


public class child extends Parent{
public void disp(){
System.out.println("in child");
}
public static void main(String[] args) {
Parent p= new child();
p.disp();
//testing tst= new Parent();
}
}

In case of  Parent class reference and child class object in Overriding there will be no compilation error and the program will execute with out put as :

in child .

Imp Notes :

No comments:

Post a Comment