Tuesday 26 March 2013

Static method hiding

              Consider the following code:

public class And
{
public static void m(){}
And(){}
public static void main(String[] args) {
And2 a=new And2();a.m();// ya this doesnt work..so i was wrong all way
}
}
class And2 extends And
{
public static void m(){}
}

Here the same method m() is being defined in two classes. The second class is not overriding the m()

method in super class but is hiding it. This means that the JVM will not decide which method to

invoke based on the object in hand but rather based on the type of reference in hand. If the reference

 is of type "And" then And's version will be called and if the reference is of type "And2" then And's

version of static method

No comments:

Post a Comment