HƯỚNG DẪN Lack of OOPL understanding I

Joe

Thành viên VIP
21/1/13
3,068
1,343
113
Hi
I found this questionKế thừa phương thức xây dựng trong java of a JAVA dev and it seems to me that the forum gurus are also in troubles to explain the "phenomena". The JAVA dev is either a self-learner newbie or a student of a very bad college that does not teach him the most fundamental basics of JAVA. The most fundamental? What is it? It's the single inheritance of ALL JAVA objects. Meaning that every extended JAVA object always inherits Object instance via the instantiattion with super(). The instantiation of an extended JAVA object is processed as following:
  1. Last Child via its constructor
  2. Parent class via its constructor if no empty Constructor exists
  3. Grand Parent via its constructor if no empty Constructor exists
  4. ...
  5. Object instance via explicit or implicit super()
The example ConNguoi.java
Java:
public class ConNguoi {
    protected String hoVaTen;
    protected int namSinh;
    public ConNguoi(String hoVaTen, int namSinh) {
        this.hoVaTen = hoVaTen;
        this.namSinh = namSinh;
    }
}
has 2 protected objects (String and int) and ONE constructor. The newbie tries to create a new object HocSinh as an extension of ConNguoi and he runs into troubles with weird error messages when class HocSinh is compiled. And the poor newbie does not know why it won't work as he has learned :-o.
Anh/Chị cho em hỏi là: Nếu lớp HocSinh kế thừa lớp ConNguoi thì lớp HocSinh sẽ kế thừa các thuộc tính và phương thức của lớp ConNguoi, cụ thể ở đây là kế thừa thuộc tính hoVaTennamSinh. Nhưng tại sao ở lớp HocSinh khi viết phương thức khởi tạo Constructor thì viết this.hoVaTen = hoVaTen; this.namsinh = namSinh; như bên dưới thì lại không được ạ, theo như em nghĩ là nó đã kế thừa được thuộc tính của lớp cha: ConNguoi rồi thì có thể gọi các thuộc tính đó nó để sử dụng chứ:
Java:
public class HocSinh extends ConNguoi {
        private String tenLop, tenTruong;
        public HocSinh(String hoVaTen, int namSinh, String tenLop, String tenTruong) {
        this.hoVaTen = hoVaTen;
        this.namSinh = namSinh;
        this.tenLop = tenLop;
        this.tenTruong = tenTruong;
    }
}
The reason is clear. Object ConNguoi has only one Constructor with 2 parameters and object HocSinh tries to use the protected field hoVaTen, namSinh without using the ConNguoi Constructor and that leads to the situation that JAVA tries to instantiate ConNguoi with the default super(), BUT ConNguoi does not have an empty constructor. So, it failed to instantiate ConNguoi. Therefore the error message with the hint of missing 2 parameters String and int for an instantiation of ConNguoi. And that is the trouble of the lack of the most fundamental basics in JAVA. However the newbie wonders why the following codes work:
Java:
public class HocSinh extends ConNguoi {
    private String tenLop, tenTruong;
    //Tạo Constructor:
    public HocSinh(String hoVaTen, int namSinh, String tenLop, String tenTruong) {
        super(hoVaTen, namSinh);
        this.tenLop = tenLop;
        this.tenTruong = tenTruong;
    }
}
Well, the super(hoVaTen, namSinh) instantiates ConNguoi (together with Object instance via implicit/explicit super()) as the super part of HocSinh and that conforms the Single Inheritance paradigm of JAVA.

If the class ConNguoi is modified as following
Java:
public class ConNguoi {
    protected String hoVaTen;
    protected int namSinh;
    // empty constructor (implicit instantiation of Object with its empty constructor)
    public ConNguoi() {  }
    //
    public ConNguoi(String hoVaTen, int namSinh) {
        this.hoVaTen = hoVaTen;
        this.namSinh = namSinh;
    }
}
OR
Java:
public class ConNguoi {
    protected String hoVaTen;
    protected int namSinh;
    // empty constructor
    public ConNguoi() {
       super(); // instantiate Object - explicit
    }
    //
    public ConNguoi(String hoVaTen, int namSinh) {
        this.hoVaTen = hoVaTen;
        this.namSinh = namSinh;
    }
}
then the object HocSinh can be now instantiated with direct use of hoVaTen and namSinh.
Java:
        this.hoVaTen = hoVaTen;
        this.namSinh = namSinh;
 
Sửa lần cuối:

Joe

Thành viên VIP
21/1/13
3,068
1,343
113
Today I found an explanation of a JAVA Guru
Java có một số cái thực hiện auto, đây là điểm mình không thích:
  • Tự động thêm default ctor (constructor) nếu class không có ctor nào
  • Trong ctor, tự động thêm dòng super(); ở đầu nếu không có lệnh super(...) nào
Trường hợp của bạn, do class ConNguoi bạn đã có ctor, nên Java không tạo thêm empty ctor. Và ctor của HocSinh không có dòng nào gọi super(...), nên Java sẽ thêm 1 dòng super(); gọi tới empty ctor của ConNguoi.
Tới đây chắc bạn đã hiểu rồi.
Totally missed! Fully balderdash! There ain't default ctor, but the constructor of object Object which is an empty constructor and is ALWAYS invoked to instantiate Object so that the child (here HocSinh or ConNguoi) inherits all Object methods such as toString(), getClass(), etc.
The worst is that the guru gave a wrong guessing answer and the newbie accepted it as a solution =))
 
  • Like
Reactions: Oberver

Joe

Thành viên VIP
21/1/13
3,068
1,343
113
Another JAVA Guru of the mentioned forum wrote:
Personally I don’t think @tonghoangvu is wrong here.
Probably Joe just misunderstood what @tonghoangvu mentioned because of language barrier.
From the doc 1:
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
It matches what @tonghoangvu explained, and clearly explain the compiler behavior (scream “Implicit super constructor ConNguoi() is undefined. Must explicitly invoke another constructor”).
=)) The guru gave an excerpt of JAVA doc as a justification of the 'auto' guru, but he kept out these lines:
If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.
First: "If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem" says that it works only if HocSinh is the only child of Object, otherwise Compile-time error. And that is the case!
A very bad interpretation from a document. Either this guru cannot completely read and understand an English document, OR he has intended to keep a fact incomplete for the only purpose to save face of his co-guru and the mentioned forum :( Further, the sentence "you need to be aware of it when there is a long line of class descent" means a chain of instantiation and that takes a lot of time -meaning: bad performance.

And that is what I mentioned above that
The instantiation of an extended JAVA object is processed as following:
  1. Last Child via its constructor
  2. Parent class via its constructor if no empty Constructor exists
  3. Grand Parent via its constructor if no empty Constructor exists
  4. ...
  5. Object instance via explicit or implicit super()
HocSinh with HocSinh(String, int, String, String) -> ConNguoi with ConNguoi(String, int) -> Object with explicit/implicit super(): 3 instances: HochSinh, ConNguoi and Object.
 
Sửa lần cuối:
  • Love
Reactions: Oberver

Oberver

New Member
4/8/22
5
5
3
Holland
Hi Joe
I am new here, but I frequently read your blogs. All the blogs are very interesting. As I gave your link of this topic to the forum you mentioned above and I got an angry answer of the moderator. He said that I am a mailman :) and recommended me to join Congdongjava. So I do. Congdongjava is better and has a higher level than Daynhauhoc which is in my belief a forum of newbies for newbies. ;)
 
  • Like
Reactions: Joe

Joe

Thành viên VIP
21/1/13
3,068
1,343
113
:))
As I gave your link of this topic to the forum you mentioned above and I got an angry answer of the moderator. He said that I am a mailman :)
You shouldn't do that. But it's OK. I have read the comment of the moderator and it seems to me that his understanding in English is mediocre. to grasp the finest details of SUN doc.
 
  • Like
Reactions: Oberver