Sunday, 29 May 2011

Runtime Polymorphism or method overriding

class A{
    public void fun1(int x){
        System.out.println(""int in Class A is : " + x : " + x);
        }
    }
    class B extends A{
        public void fun1(int x){
            System.out.println("int in Class B is : " + x);
            }
        }
        public class polytwo{
            public static void main(String[] args){
                A obj;
                obj=new A(); // line 1
                obj.fun1(2);  // line 2 (prints ""int in Class A is : 2")
                obj=new B();  // line 3
                obj.fun1(5);  // line 4 (prints "int in Class A is : 5")
                }
        }
int in class A is: 2
int in class B is:5

No comments: