Sunday, 29 May 2011

Compline time Polymorpism or method overloading

class A{
    public void fun1(int x){
        System.out.println("The value of class A is : " + x);
    }
    public void fun1(int x,int y){
        System.out.println("The value of class B is : "+x +" and "+ y);
    }
}
public class polyone{
    public static void main(String[] args){
        A obj=new A();
        // Here compiler decides that fun1(int) is to be called and "int" will be printed.
        obj.fun1(2);
        // Here compiler decides that fun1(int,int) is to be called and "int and int" will be printed.
        obj.fun1(2,3);
    }
}

output

The value of class A is: 2
The value of class B is: 2 and 3

No comments: