Java Dynamic Binding

I am having trouble understanding the OOP Polymorphic principl of Dynamic Binding ( Late Binding ) in Java. I looked for question pertaining to java, and wasn’t sure if a overall answer to how dynamic binding works would pertain to Java Dynamic Binding, I wrote this question.

Given:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Person
{
private String name;
Person(intitialName)
{
name = initialName;
}
// irrelevant methods is here.
// Overides Objects method
public void writeOutput()
{
println(name);
}
}
class Student extends Person
{
private int studentNumber;
Student(String intitialName, int initialStudentNumber)
{
super(intitialName);
studentNumber = initialStudentNumber;
}
// irrellevant methods here...
// overides Person, Student and Objects method
public void writeOutput()
{
super.writeOutput();
println(studentNumber);
}
}
class Undergaraduate extends Student
{
private int level;
Undergraduate(String intitialName, int initialStudentNumber,int initialLevel)
{
super(intitialName,initialStudentNumber);
level = initialLevel;
}
// irrelevant methods is here.
// overides Person, Student and Objects method
public void writeOutput()
{
super.writeOutput();
println(level);
}
}
</code>
<code>class Person { private String name; Person(intitialName) { name = initialName; } // irrelevant methods is here. // Overides Objects method public void writeOutput() { println(name); } } class Student extends Person { private int studentNumber; Student(String intitialName, int initialStudentNumber) { super(intitialName); studentNumber = initialStudentNumber; } // irrellevant methods here... // overides Person, Student and Objects method public void writeOutput() { super.writeOutput(); println(studentNumber); } } class Undergaraduate extends Student { private int level; Undergraduate(String intitialName, int initialStudentNumber,int initialLevel) { super(intitialName,initialStudentNumber); level = initialLevel; } // irrelevant methods is here. // overides Person, Student and Objects method public void writeOutput() { super.writeOutput(); println(level); } } </code>
class Person
{
     private String name;

     Person(intitialName)
     {
          name = initialName;
     }

     // irrelevant methods is here.

     // Overides Objects method
     public void writeOutput()
     {
          println(name);
     }
}

class Student extends Person
{

     private int studentNumber;

     Student(String intitialName, int initialStudentNumber)
     {
     super(intitialName);
     studentNumber = initialStudentNumber;
     }

     // irrellevant methods here...

     // overides Person, Student and Objects method
     public void writeOutput()
     {
      super.writeOutput();
      println(studentNumber);
     }   
  }

class Undergaraduate extends Student
{

     private int level;

     Undergraduate(String intitialName, int initialStudentNumber,int initialLevel)
     {
     super(intitialName,initialStudentNumber);
     level = initialLevel;
     }

     // irrelevant methods is here.

     // overides Person, Student and Objects method
     public void writeOutput()
     {
      super.writeOutput();
      println(level);
     }

  }

I am wondering. if I had an array called person declared to contain objects of type Person:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> Person[] people = new Person[2];
person[0] = new Undergraduate("Cotty, Manny",4910,1);
person[1] = new Student("DeBanque, Robin", 8812);
</code>
<code> Person[] people = new Person[2]; person[0] = new Undergraduate("Cotty, Manny",4910,1); person[1] = new Student("DeBanque, Robin", 8812); </code>
  Person[] people = new Person[2];
  person[0] = new Undergraduate("Cotty, Manny",4910,1);
  person[1] = new Student("DeBanque, Robin", 8812);

Given that person[] is declared to be of type Person, you would expect, for example, in the third line where person[0] is initialized to a new Undergraduate object,to only gain the instance variable from Person and Persons Methods since doesn’t the assignment to a new Undergraduate to it’s ancestor denote the Undergraduate object to access Person – it’s Ancestors, methods and isntance variables…

Thus …with the following code I would expect

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> person[0].writeOutput(); // calls Undergraduate::writeOutput()
person[1].writeOutput(); // calls Student::writeOutput()
</code>
<code> person[0].writeOutput(); // calls Undergraduate::writeOutput() person[1].writeOutput(); // calls Student::writeOutput() </code>
  person[0].writeOutput();  // calls Undergraduate::writeOutput()
  person[1].writeOutput();  // calls Student::writeOutput() 

person[0] to not have Undergraduate’s writeOutput() overidden method, nor have person[1] to have Student’s overidden method – writeOutput().

If I had

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Person mikeJones = new Student("Who?,MikeJones",44,4);
mikeJones.writeOutput();
</code>
<code>Person mikeJones = new Student("Who?,MikeJones",44,4); mikeJones.writeOutput(); </code>
Person mikeJones = new Student("Who?,MikeJones",44,4);
mikeJones.writeOutput();

The Person::writeOutput() method would be called.

Why is this not so? Does it have to do with something I don’t understand about relating to arrays? Does the declaration Person[] people = new Person[2] not bind the method like the previous code would?

1

It has nothing to do with arrays, it is the effect of the “late binding”. The Java compiler only checks that Person has the writeOutput method, but does not bind the call to that class method. The binding is performed at runtime, where the JVM finds that Person[0] is an UnderGraduate and calls the method definition from UnderGraduate, and not the one from Person.

I understand that C++ works the other way unless you specify virtual, but it is better this way (if you think that a class encapsulates data and methods, you ensure that the methods used are the ones which really matter to the encapsulated data).

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật