Function_overriding in Java vs C++

Posted on 2015-06-22 17:27:24 +0900 in Programming Java C++

Similar code, different behaviour

Quesion in stackoverflow:

Java version:

class base{
    public void func1(){
        func2();
    }
    public void func2(){
        System.out.println(" I am in base:func2() \n");
    }

}

class derived extends base{
    public void func1(){
        super.func1();
    }
    public void func2(){
        System.out.println(" I am in derived:func2() \n");
    }
};
public class Test
{
    public static void main(String[] args){
        derived d = new derived();
        d.func1();
    }
}

Output:

I am in derived:func2()

C++ version

#include <stdio.h>

class base
{
    public:
        void func1(){
            func2();
        }
        void func2(){
            printf(" I am in base:func2() \n");
        }
};

class derived : public base
{
    public:
        void func1(){
            base::func1();
        }
        void func2(){
            printf(" I am in derived:func2() \n");
        }
};

int main()
{
    derived *d = new derived();
    d->func1();
    return 0;
}

Output:

I am in base:func2()

Why

Firstly, I thought the behaviour of C++ is reasonable, Java version is hard to understand, as I am more used to static binding.

For both languages, variable binding is static, no matter it is member variable or not.

But for method binding, two languages behaviour differently.

In C++, unless this method is virtual, otherwise it is bind staticlly default.

but in Java, all method bindings use late binding unless it is static or final(private is implicly final).

Java version behaves as if you had declared base::func2 as

virtual void func2(){
    printf(" I am in base:func2() \n");
}

Static Binding vs Late Binding

static binding means references are resovled at compile time, namely it is possible to resolve it by its position.

late binding means references are resolved at run time, which is implemented through Vtable in C++.

----------------------------------- 本文内容遵从CC版权协议转载请注明出自kamelzcs -----------------------------------
«  | SRM 661 div1 »

Hide Comments

comments powered by Disqus