简体   繁体   中英

Confused between dynamic binding and constructor chaining in java

According to the book I am following, the following code print:

public class DynamicBindingDemo {
   public static void main(String[] args) {

   m(new graduateStudent());
   m(new Student());
   m(new Person());
   m(new Object());
 }

    public static void m(Object x) {
    System.out.println(x.toString());
   }
}

   class GraduateStudent extends Student {
   }

    class Student extends Person {
       public String toString() {
       return "Student";
     }
 }

  class Person extends Object {
   public String toString() {
     return "Person";
   }
 }

Prints:

Student

Student

Person

java.lang.Object@130c19b

Can someone please help me explain why? Here how I traced the code:

So m(new graduateStudent()) invokes a polymorphic call to m(Object x), the x.toString() is invoked by GraduateStuent class instead of Object class because GraduateStuent is the actual type of the object. Before x.toString() can be invoked in GraduateStudent class, the compiler creates a no arg constructor implicitly in the GS class because there isnt one. However, before this constructor can be invoked, student class constructor is invoked first because it is the superclass. Student constructor is created in the student class(because it doesnt have one), but before that constructor can be invoked Persons constructor is invoked because it is the superclass and so on till we reach the Object superclass and its to string methods should be invoked....

I am pretty sure I am wrong on many things(I am a college freshman of 2 months who is a little of the course). So can someone explain what i am getting wrong?

Which method is called doesn't depend on constructor execution. There is a trick however, parent's constructor is executed as a first step in the child's constructor. It will be added by the compiler unless you add it explicitly. Eg Student's constructor looks like this:

public Student() {
  super(); //parent initialised before child
}

What you actually do is a method overriding.

You can think of overriding as of replacing a method in any parent class. Also to understand this you can think of inheritance chain: Graduate -> Student -> Person -> Object

The first toString method found in the chain will be called. Actually this version is used in prototype inheritance, in java it's implemented differently. But for understanding it should be sufficient.

So regarding you example:

  • You are not overriding parent method of parent Student class so Student's method is called
  • Student overrides Person toString method, Student's method is called
  • Person overrides Object's toString method, Person's method is called
  • Object's toString is called as it has no Parent class and contains method toString.

You can read more about method overriding in the Internet :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM