Java: Từ khóa super
Giải phóng thời gian, khai phóng năng lực
1. Truy cập các thành phần của lớp cha
Nếu phương thức của lớp con ghi đè phương thức của lớp cha thì ta có thể gọi phương thức bị ghi đè đó của lớp cha bằng cách sử dụng từ khóa super
. Ta cũng có thể sử dụng từ khóa super
để tham chiếu tới một thuộc tính ẩn nào đó (mặc dù thuộc tính ẩn là không khuyến khích) của lớp cha.
Ví dụ ta có lớp Animal như sau:
//tạo lớp Animal: public class Animal { //khai báo các thuộc tính: int id; //mã nhận diện String name; //tên float age; //tuổi void inputInfo(){//dùng để nhập liệu Scanner input=new Scanner(System.in); System.out.print("Input id: "); id=input.nextInt(); System.out.print("Input name: "); input.nextLine(); name=input.nextLine(); System.out.print("Input age: "); age=input.nextFloat(); } void showInfo() {//dùng để hiển thị System.out.println("ID: " + id); System.out.println("Name: " + name); System.out.println("Age: " + age); } }
Bây giờ ta cần xây dựng lớp Cat thừa kế từ lớp Animal ở trên:
class Cat extends Animal{ String color; //màu lông }
Thì lớp Cat sẽ được thừa kế các thuộc tính id, name, age, và thừa kế các phương thức inputInfo() và showInfo().
Tuy nhiên, phương thức inputInfo() của lớp cha chỉ nhập liệu được cho 3 thuộc tính id, name và age, không thể nhập liệu được cho thuộc tính color.
Để giải quyết điều này thì ta làm như sau:
class Cat extends Animal{ String color; @Override void inputInfo() { Scanner input=new Scanner(System.in); super.inputInfo();//dùng từ khóa super để gọi phương thức bị ghi đè của lớp cha System.out.print("Input color: "); color=input.nextLine(); } }
Ở đoạn code trên, từ khóa super được dùng để gọi phương thức bị ghi đè của lớp cha là inputInfo().
Tương tự, phương thức showInfo() của lớp cha Animal cũng không hiển thị được thông tin thuộc tính color của lớp con Cat.
Trong trường hợp này ta cũng định nghĩa phương thức showInfo() cho lớp Cat và dùng từ khóa super để gọi phương thức showInfo() của lớp cha Animal như sau:
class Cat extends Animal{ String color; @Override void inputInfo() { Scanner input=new Scanner(System.in); super.inputInfo(); System.out.print("Input color: "); color=input.nextLine(); } @Override void showInfo() { super.showInfo();//dùng từ khóa super để gọi phương thức showInfo của lớp cha System.out.println("Color: "+color); } }
Dưới đây là chương trình hoàn chỉnh, bạn có thể copy và chạy thử:
import java.util.Scanner; //tạo lớp Animal: public class Animal { //khai báo các thuộc tính: int id; //mã nhận diện String name; //tên float age; //tuổi void inputInfo(){ Scanner input=new Scanner(System.in); System.out.print("Input id: "); id=input.nextInt(); System.out.print("Input name: "); input.nextLine(); name=input.nextLine(); System.out.print("Input age: "); age=input.nextFloat(); } void showInfo() { System.out.println("ID: " + id); System.out.println("Name: " + name); System.out.println("Age: " + age); } } class Cat extends Animal{ String color; @Override void inputInfo() { Scanner input=new Scanner(System.in); super.inputInfo();//dùng từ khóa super để gọi phương thức inputInfo() của lớp cha System.out.print("Input color: "); color=input.nextLine(); } @Override void showInfo() { super.showInfo();//dùng từ khóa super để gọi phương thức showInfo() của lớp cha System.out.println("Color: "+color); } } class TestCat { public static void main(String[] args) { Cat cat = new Cat(); cat.inputInfo(); cat.showInfo(); } }
2. Truy cập hàm tạo của lớp cha từ lớp con
Bây giờ, lớp cha Animal ở trên được thêm vào hai hàm tạo không tham số và ba tham số như sau:
//tạo lớp Animal: public class Animal { //khai báo các thuộc tính: int id; //mã nhận diện String name; //tên float age; //tuổi //hàm tạo không tham số public Animal() { id = 1234; name = "Moon"; age = 1; } //hàm tạo 3 tham số public Animal(int id, String name, float age) { this.id = id; this.name = name; this.age = age; } void inputInfo(){ Scanner input=new Scanner(System.in); System.out.print("Input id: "); id=input.nextInt(); System.out.print("Input name: "); input.nextLine(); name=input.nextLine(); System.out.print("Input age: "); age=input.nextFloat(); } void showInfo() { System.out.println("ID: " + id); System.out.println("Name: " + name); System.out.println("Age: " + age); } }
Và lớp Cat vẫn thừa kế từ lớp Animal.
Thì vì lớp con không được phép thừa kế hàm tạo của lớp cha, cho nên nếu muốn thì ta phải xây dựng hàm tạo không tham số cho lớp con Cat, và nếu muốn ta cũng phải xây dựng hàm tạo bốn tham số (ba tham số id, name, age thừa kế từ lớp cha Animal và một tham số color của Cat) cho lớp con Cat.
Tuy nhiên, nếu ta muốn tận dụng những hàm tạo của lớp cha Animal thay vì phải xây dựng từ đầu, thì lúc này ta có thể làm như sau:
class Cat extends Animal{ String color; //hàm tạo 0 tham số của lớp con Cat public Cat(){ super();//dùng super để gọi tới hàm tạo 0 tham số của lớp cha Animal color="Trắng"; } //hàm tạo 4 tham số của lớp con Cat public Cat(int id, String name, float age, String color) { super(id, name, age);//dùng super để gọi tới hàm tạo 3 tham số của lớp cha Animal this.color = color; } @Override void inputInfo() { Scanner input=new Scanner(System.in); super.inputInfo(); System.out.print("Input color: "); color=input.nextLine(); } @Override void showInfo() { super.showInfo();//dùng từ khóa super để gọi phương thức showInfo của lớp cha System.out.println("Color: "+color); } }
Sau đây là ví dụ đầy đủ, bạn có thể copy và chạy thử:
import java.util.Scanner; //tạo lớp Animal: public class Animal { //khai báo các thuộc tính: int id; //mã nhận diện String name; //tên float age; //tuổi //hàm tạo không tham số public Animal() { id = 1234; name = "Moon"; age = 1; } //hàm tạo 3 tham số public Animal(int id, String name, float age) { this.id = id; this.name = name; this.age = age; } void inputInfo(){ Scanner input=new Scanner(System.in); System.out.print("Input id: "); id=input.nextInt(); System.out.print("Input name: "); input.nextLine(); name=input.nextLine(); System.out.print("Input age: "); age=input.nextFloat(); } void showInfo() { System.out.println("ID: " + id); System.out.println("Name: " + name); System.out.println("Age: " + age); } } class Cat extends Animal{ String color; //hàm tạo 0 tham số của lớp con Cat public Cat(){ super();//dùng super để gọi tới hàm tạo 0 tham số của lớp cha Animal color="Trắng"; } //hàm tạo 4 tham số của lớp con Cat public Cat(int id, String name, float age, String color) { super(id, name, age);//dùng super để gọi tới hàm tạo 3 tham số của lớp cha Animal this.color = color; } @Override void inputInfo() { Scanner input=new Scanner(System.in); super.inputInfo(); System.out.print("Input color: "); color=input.nextLine(); } @Override void showInfo() { super.showInfo();//dùng từ khóa super để gọi phương thức showInfo của lớp cha System.out.println("Color: "+color); } } class TestAnimal { public static void main(String[] args) { //dùng hàm tạo 0 tham số Cat cat = new Cat(); cat.showInfo();//hiển thị thông tin //dùng hàm tạo 4 tham số cat=new Cat(5678,"Bengal",2.5f,"Nâu đốm"); cat.showInfo();//hiển thị thông tin } }
Kết quả:
ID: 1234 Name: Moon Age: 1.0 Color: Trắng ID: 5678 Name: Bengal Age: 2.5 Color: Nâu đốm
Lưu ý: Nếu hàm tạo không gọi tường minh tới hàm tạo của lớp cha thì trình biên dịch Java sẽ tự động chèn một lời gọi tới hàm tạo không đối số của lớp cha. Lúc này, nếu lớp cha mà không có hàm tạo không tham số thì bạn sẽ nhận được một lỗi.
Giải phóng thời gian, khai phóng năng lực