Dynamic method dispatch & Override

 

package overriding;

class Super

{

    public void meth1()

    {

        System.out.println(” meth1″);

    }

    public void meth2()

    {

        System.out.println(“meth2”);

    }

}

class Sub extends Super

{

    @Override

    public void meth2()

    {

        System.err.println(“sub meth2”);

    }

    public void meth3()

    {

        System.out.println(“meth3”);

    }

}

public class Overriding {

    

    public static void main(String[] args) {

       Super s1=new Super();

       

       Super s2=new Sub();//here Super is refrence & su object

       s2.meth1();//super class meth1 will call

       s2.meth2();//Sub class override version of meth2 will call

       

    }

    

}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top