يوسع Java Keyword
مثال
ترث الفئة Car
(فئة فرعية) السمات والطرق من Vehicle
الفئة (فئة فائقة):
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {
// Create a myCar object
Car myCar = new Car();
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
التعريف والاستخدام
تمتد extends
الكلمة الأساسية فئة (تشير إلى أن فئة موروثة من فئة أخرى).
في Java ، من الممكن أن ترث السمات والطرق من فئة إلى أخرى. نقوم بتجميع "مفهوم الميراث" في فئتين:
- الفئة الفرعية (الطفل) - الفئة التي ترث من فئة أخرى
- الطبقة الفائقة (الأصل) - الطبقة الموروثة منها
لترث من فصل دراسي ، استخدم extends
الكلمة الأساسية.
الصفحات ذات الصلة
اقرأ المزيد حول الوراثة في برنامج Java Inheritance التعليمي .