-1

I do have class Person, class Student and Student extends Person. As far as I understood, it goes the following with static binding:

class Person {
   talk(Person p) {
      print("Hi by person.");
   } 
}

class Student extends Person {
   talk(Student s) {
      print("Hi by stud.");
   }
}

Now if I instantiate and call method:

Person x = new Student();
talk(x);                      
// output: "Hi by person." because of static binding, am I right?

My Question: What if only class Student has a method talk(Student s). Now I call talk(x). Since I usually should get talk() method from class Person, what happens when there is no such method?

EDIT: I tried to run it and it gives me an Compile Error. Ok, but why does this happen? I learned that the compiler will first go to the subclass and search for the method and if it's there, then it gets executed?

Jush KillaB
  • 151
  • 1
  • 11

1 Answers1

0

Don't exist dynamic binding for overloaded methods ...

and Student is a Person so method talk from Person invoked

Yashar Panahi
  • 2,816
  • 1
  • 16
  • 22
  • This is rather far from being clear... For example "_`Student` is a `Person`_" is really meaning that the reference `x` is a reference to a `Person`, not that `Student extends Person`. – Boris the Spider Mar 18 '18 at 20:55