1/7
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is inheritance?
Inheritance allows a class (subclass) to acquire properties and behaviors from another class (superclass).
Kế thừa cho phép một lớp (lớp con) nhận các thuộc tính và hành vi từ một lớp khác (lớp cha).
What are the benefits of inheritance?
Inheritance allows a new class to reuse fields and methods of an existing class, reducing code duplication. It improves code structure, readability, and maintainability. The prime benefit is code reusability.
Kế thừa cho phép lớp mới tái sử dụng thuộc tính và phương thức của lớp có sẵn, giảm trùng lặp mã. Nó giúp cải thiện cấu trúc, dễ đọc và dễ bảo trì mã. Lợi ích chính là tái sử dụng mã.
What is polymorphism? Can you give an example?
Polymorphism is the ability of objects to be treated as instances of their parent class but behave differently based on their actual class.
Đa hình là khả năng đối tượng được xử lý qua lớp cha chung nhưng thực thi hành vi khác nhau tùy kiểu thực tế của đối tượng.
class Animal {
void sound() {
System.out.println("Tiếng chung");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Gâu gâu");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Meo meo");
}
}
public class Test {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.sound(); // in ra "Gâu gâu" // ĐA HÌNH
a2.sound(); // in ra "Meo meo" // ĐA HÌNH
}
}
Is multiple class inheritance supported in Java?
No, Java does not support multiple inheritance with classes to avoid ambiguity issues (like the diamond problem). Instead, Java allows multiple inheritance of types via interfaces.
Không, Java không hỗ trợ kế thừa đa lớp để tránh sự mơ hồ. Thay vào đó, Java hỗ trợ đa kế thừa kiểu qua interfaces.
Is multiple interface inheritance supported in Java?
Yes, Java supports multiple inheritance of interfaces. A class can implement multiple interfaces, allowing it to inherit abstract methods from all of them.
Có, Java cho phép một lớp implement nhiều interface cùng lúc, kế thừa các phương thức trừu tượng từ tất cả interface đó.
What is the diamond problem?
The diamond problem occurs in multiple inheritance when a class inherits the same method from two classes, causing ambiguity about which method to use.
Diamond problem xảy ra khi kế thừa đa lớp, một lớp con kế thừa phương thức giống nhau từ hai lớp cha khác nhau, gây ra sự mơ hồ phương thức nào sẽ được gọi.
example ?
public abstract class Language {
public abstract void sayHello();
}
public class Punjabi extends Language {
String lang = "punjabi";
public void sayHello() {
System.out.println("Kiddaan");
}
}
public class Marathi extends Language {
String lang = "marathi";
public void sayHello() {
System.out.println("Namaskaar");
}
}
public class BiLingual extends Punjabi, Marathi {
public void greet() {
super.sayHello();
}
}
Can the diamond problem occur if we use interfaces instead of classes?
No, Java avoids the diamond problem with interfaces because interfaces can only have abstract methods (no implementation). Since there’s no method body to inherit, there’s no ambiguity. If default methods are used, Java requires the class to override them explicitly to resolve conflicts.
Không, vì interface chỉ có phương thức trừu tượng nên không có phần cài đặt để mơ hồ. Nếu interface có default methods trùng nhau, lớp con phải override để giải quyết xung đột.