Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function overriding in Java vs C++

Two similar definitions in Java and C++, but totally different behaviour.

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()

I don't know why they have different behaviour.

Even I know Java has auto polymorphism behaviour.

The Java output is hard to understand personally.

In my view, according to static scope, the base class function func1() should only be able to call the base class function func2(), as it knows nothing about the derived class at all. Otherwise the calling behaviour belongs to dynamic scope. Maybe in C++, func2() in base class is bind static, but in Java it is bind dynamic?

Member field is statically scoped.


The type inferring part is confusing. I thought this is converted to base type in the base::func1(). In C++, the base::func2() is not polymorphism, so the base::func1() is called. While in Java, base::func2() is polymorphism, so devried::func2() is called.

How the func2() class binding being inferred? Or Which fun2() should be called and how it is determined.

What happened behind base::func1()? Is there any cast here for this (from derive to base)? If no, how this is able to reach to the function in base class?

        void func1(){
            func2();
        }

Useful discussion on coderanch.

like image 675
Kamel Avatar asked Jun 17 '15 06:06

Kamel


1 Answers

In Java all methods that can be overridden are automatically virtual. There is no opt-in mechanism (virtual keyword) for it as it is in C++ (and there's no way to opt-out either).

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

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

In which case your program would have printed "I am in derived:func2()".

How the func2() class binding being inferred?
Which fun2() should be called and how it is determined.

For non-virtual methods (C++ methods without virtual modifier) it is the static type that determines which method to call. The static type of the variable is determined by the variable declaration and does not depend on how the code is executed.

For virtual methods (C++ methods with the virtual modifier and all Java methods) it is the runtime type that determines which method to call. The runtime type is the type of the actual object in runtime.

Example: If you have

Fruit f = new Banana();

the static type of f is Fruit and the runtime type of f is Banana.

If you do f.someNonVirtualMethod() the static type will be used and Fruit::someNonVirtualMethod will be called. If you do f.someVirtualMethod() the runtime type will be used and Banana::someVirtualMethod will be called.

The underlying implementation for how the compiler achieves this is basically implementation dependent, but typically a vtable is used. For details refer to

  • How Vtable of Virtual functions work
  • How does virtual method invocation work in C++?
  • Mechanism of Vptr and Vtable in C++

If no, how this is able to reach to the function in base class?

void func1(){
    func2();
}

If you're wondering why func2() here calls base's func2 it is because

A) You're in the scope of base which means that the static type of this is base, and

B) func2 in base is not virtual, so it is the static type that decides which implementation to call.

like image 150
aioobe Avatar answered Oct 17 '22 15:10

aioobe