200+ câu trắc nghiệm Java cơ bản có đáp án - Phần 3

Đoạn mã sau có lỗi biên dịch nào? class Student { public String sayHello(){ return "Student"; } } public class Man extends Student{ public int sayHello() { return 0; } } Chọn một Câu trả lời

17/31

Đoạn mã sau có lỗi biên dịch nào?

class Student {

public String sayHello(){

return "Student";

}

}

public class Man extends Student{

public int sayHello() {

return 0;

}

}

Chọn một Câu trả lời

Có lỗi biên dịch:sayHello() in Man cannot override sayHello() in Student return type int is not compatible with java.lang.String

Không có lỗi biên dịch.

Có lỗi biên dịch:sayHello() in Man cannot override sayHello() in Student because it is final

Có lỗi biên dịch:sayHello() in Man and in Student has the same access modifier

Giải thích

Chọn đáp án A

Vì:

Lớp Man định viết đè sayHello() được viết ở Student, nhưng có kiểu dữ liệu trả về là int không thể chuyển thành String được.

Và sayHello() không được đánh dấu là final, nên có thể được ghi đè.

sayHello() ở cả hai lớp đều có cùng access modifier, điều này là hợp lệ.