Monday, February 9, 2009

Method Overriding

If you don’t want parent class implementation for any method we can override in the child class based on our child class requirement. This concept is called Overriding.
While overriding we have to fallow rules:
1. In the overriding, the method names and arg’s must be same.
Ie. In the case of the overriding the signatures of the methods must be same
Until 1.4 version, the return types must be same. But from 1.5 version onwards covariant return types are also allowed.
Example:
class p { public Number getNumber(){} }
class c extends P{ public Number getNumber(){} }

(or)
class C extends P
{
public Byte/Short/Integer getNumber(){}
}
  • For Number class Byte/Short/Integer is co-variant return types or classes.
  • Hence in case of overriding as the return type we can keep child class objects also.
Example:
import java.io.*;
class P
{
public object m1(){ return new object; }
}
class C extends P
{
public Object m1(){ return new Object; }
}
class C extends P
{
public String m1(){ return “durga”; }
}//in 1.4 CTE saying m1() in C cannot override found:string, req:object: in 1.5 ver no CTE

2. final methods can’t be overridden.
3. private methods never participate in the in the overriding because these methods are not visible in the child classes.
4. While overriding decreasing access specifier is not allowed. Violation leads to CTE.

class P { public int m1(){} }
class C extends P { public int m1(){} }
Parent class -------------------------------- Child class
public ----------------------------------------------- public
protected ---------------------------------------- protected, public
default ---------------------------------------- default, protected ,public
private ---------------------------------------- private, default, protected, public

* While implementing any interface method, we should declare that method as public in the implemented class.(by default implements interface method is public and abstract)
* An abstract method can be overridden as abstract. The child of the original child class is responsible for the implementation.

class P{
public void m1()
{ } }//non abstract

abstract class C extends P{
public abstract m1(); }//valid abstract


5. While overriding the size of the CheckedException should not increase. There is no rule for UnCheckedExceptions.
Example: Base or parent class :
public void m1()throws IOException
Derived or child class :

public void m1()throws FileNotfoundException //valid

public void m1()throws Exception -->CTE

public void m1()throws RunTimeException //valid

  • We can override a synchronized method to non-synchronized and vice versa
  • We can override native to native to non native and vice versa.
  • We can override a non-final method to final method.
  • We can’t override a static method to non static and a non-static method to static violation leads to CTE .
  • While overriding method has to execute will be decided by JVM based on the Run Time Object. Hence Overriding is an example of “dynamic polymorphism” or “LateBinding” (Dynamic Method dispatch).
  • If the parent’s class reference can be used to hold child class object by using that reference we are allowed to call only parent class methods. Child class specific methods are not allowed to call by using parent class reference.
Method Hiding:-
This is exactly same as overriding except both parent & child class methods must be declared as static. In the method hiding the method resolution take care by compiler only based on the reference type.
Ex:
class P
{
static int x=10;
int y=20;
}
class C extends P
{
static int x=100;
int y=200;
}
class Sample
{
public static void main(String[] a)
{
P p=new C();
System.out.println(p.x+”,”+p.y); //10,20
C c=new C();
System.out.println(c.x+”,”+c.y); //100,200
P p1=new P();
System.out.println(p1.x+”,”+p1.y); //10,20
}
}
  • We can’t override in the child class. But if define exactly same variable in child class.
  • Variable resolutions take care by compiler only based on the reference type.

3 comments:

Anonymous said...

it is good ,thanks for ur block

Unknown said...

it's veryu good for interview point of view


thankyou
venki

Anonymous said...

horrible post it's like a monkey wrote it

Post a Comment