Question from the Object-oriented programming - Fundamentals test

In this code, the class B extends the class A. The class B overrides the method talk() from the class A. The object b is an instance of the class B. The object a is an instance of the class A. The object a is assigned to the object b. The object a calls the method talk(). The method talk() from the class B is called.

Medium

What does the following code :

public class A {
    public void talk() {
        echo "Hello my name is A"
    }
}


public class B extends A {
    public void talk() {
        echo "Hello my name is B"
    }
}

B b = new B();
A a = b;
a.talk();

Author: SamuelStatus: PublishedQuestion passed 511 times
Edit
1
Community Evaluations
developer avatar
Georget CAMY
22/06/2024
En c++, cette question est problématique : Si la méthode talk() de la classe A est virtuelle alors effectivement, la sortie est bien "Hello my name is B". Cependant, si la méthode talk() n'est pas virtuelle, alors la sortie est "Hello my name is A".
developer avatar
Auteur anonyme
12/01/2023
"B b = new B():" the end of the line is not ":", it must end with ";". Answer is wrong, The output is B.