What is virtual method calling in java?

前端 未结 4 628
故里飘歌
故里飘歌 2020-12-01 13:05

Ive saw the next paragraph in some computer science test and i\'ll hope i can get here a good explanation of what it means because i googled it for an hour and can\'t find a

4条回答
  •  情书的邮戳
    2020-12-01 13:16

    The author of these lines used the c++ terminology of virtual.

    A better terminology is dynamic binding / dynamic dispatch.

    That means, that the object's dynamic type is "chosing" which method will be invoked, and not the static type.

    For example: [pseudo code]:

    class A {
      public void foo() { }
    }
    class B extends A { 
      public void foo() { }
    }
    

    when invoking:

    A obj = new B();
    obj.foo();
    

    B.foo() will be invoked, and NOT A.foo(), since the dynamic type of obj is B.

提交回复
热议问题