Jush KillaB
Jush KillaB

Reputation: 151

Static/dynamic binding in Java

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?

Upvotes: -1

Views: 55

Answers (1)

Yashar Panahi
Yashar Panahi

Reputation: 3514

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

and Student is a Person so method talk from Person invoked

Upvotes: 0

Related Questions