Kế Thừa Trong Java - Techacademy

// File Student.java public class Student { int id; String name; Address address; Student(int id, String name, Address address) { this.id = id; this.name = name; this.address = address; } void display() { System.out.println("ID: " + id); System.out.println("Name: " + name); System.out.println("Address: " + address.city + " " + address.country + " " + address.zipcode); } } // File Main.java public class Main { public static void main(String[] args) { Address address = new Address("HCM", "VN", 11000); Student student = new Student(1, "Hai", address); student.display(); } }

Output:

ID: 1 Name: Hai Address: HCM VN 11000

VI. Bài Tập Kế Thừa Trong Java Có Lời Giải

Tạo 1 lớp Person lưu trữ các thông tin dưới đây ( Tên, giới tính, địa chỉ,ngay sinh)

Tạo 1 lớp Student kế thừa từ lớp person lưu trữ các thông tin như dưới đây:

Mã sinh viên,điểm trung bình, email

Viết 1 phương thức nhập thông tin của student

Viết 1 phương thức hiện thị thông tin của student

Viết phương trình xem xét có sinh viên nào được học bổng không? điểm trung bình hơn 8.0 sẽ được hổng bổng

Khai báo class parent Person

class Person{ protected String name; protected String sex; protected String adrress; protected String ngaysinh; public void intputPerson(){ Scanner scanner = new Scanner(System.in); System.out.println("Nhap Ten :"); this.name = scanner.nextLine(); System.out.println("Nhap gioi tinh"); this.sex = scanner.nextLine(); System.out.println("Nhap dia chi"); this.adrress = scanner.nextLine(); System.out.println("Nhap ngay sinh"); this.ngaysinh = scanner.nextLine(); } public void showPerson(){ System.out.println("Ho Ten : " +this.name + " Gioi Tinh : " +this.sex +" Dia chi : " +this.adrress +" Ngay sinh : " +this.ngaysinh); } }

Trong đấy:

Có 4 biến bao gồm ( ten,gioitinh,diachi,ngaysing) và có 2 phương thức:

intputPerson : Là phương thức nhập thông tin

showPerson : Xuất hiện thông tin

Tiếp thep khai báo 1 lớp student được kế thừa từ lớp Person

class Student extends Person{ protected String masv; protected float diemtrungbinh; protected String email; public void intPutStudent(){ Scanner input = new Scanner(System.in); System.out.println("Nhap ma sinh vien"); this.masv = input.nextLine(); System.out.println("Nhap diem trung binh"); this.diemtrungbinh = input.nextFloat(); System.out.println("Nhap email"); this.email = input.nextLine(); } public void showStudent(){ System.out.println("Ma sinh vien : " +this.masv +" Diem trung binh " + this.diemtrungbinh); } public void kiemtrahocbong(){ if(this.diemtrungbinh >=8){ System.out.println("Duoc hoc bong"); }else{ System.out.println("Khong duoc hoc bong"); } } }

Lớp Person có 3 biến cần lưu trữ,và 3 phương thức java

intPutStudent : Nhập thông tin của sinh viên đấy.

showStudent : Xuất thông tin của sinh viên

kiemtrahocbong : Phương thức kiểm tra học bổng

Hàm khởi tạo để thực hiện chương trình như sau:

ublic class BT1 { public static void main(String []args){ Student person = new Student(); person.intputPerson(); person.intPutStudent(); person.showPerson(); person.showStudent(); person.kiemtrahocbong(); } }

Kết quả:

VII. Đa Kế Thừa Trong Java

Nếu 1 lớp triển khai đa kế thừa, hoặc 1 Interface kế thừa từ nhiều Interface thì đó là đa kế thừa.

Trong Java, 1 lớp chỉ được thừa kế (extends) từ 1 lớp, có thể cài đặt (implements) nhiều interface. Tuy nhiên, 1 interface có thể thừa kế (extends) nhiều interface.

1 interface không thể cài đặt (implements) interface khác, do interface không phần cài đặt, chỉ chứa các khai báo.

Ví dụ 1 lớp cài đặt (implements) nhiều interface:

public interface Shape { void draw(); } public interface Color { String getColor(); } public class Rectangle implements Shape, Color { @Override public void draw() { System.out.println("Draw " + this.getColor() + " rectangle"); } @Override public String getColor() { return "red"; } }

Ví dụ interface kế thừa (extend) nhiều interface:

public interface Shape { void draw(); } public interface Color { String getColor(); } public interface ShapeColor extends Shape, Color { } public class Circle implements ShapeColor { @Override public void draw() { System.out.println("Draw " + this.getColor() + " circle"); } @Override public String getColor() { return "red"; } }

Câu hỏi: Đa kế thừa không được hỗ trợ thông qua lớp trong Java nhưng là có thể bởi Interface, vì sao?

Như đã giới thiệu, kế thừa không được hỗ trợ thông qua lớp. Nhưng nó được hỗ trợ bởi Interface bởi vì không có tính lưỡng nghĩa khi trình triển khai được cung cấp bởi lớp Implementation.

Ví dụ đa thừa kế với Interface

public interface Printable { void print(); } public interface Showable { void print(); } public class InterfaceDemo implements Printable, Showable { public void print() { System.out.println("Welcome to gpcoder.com"); } public static void main(String args[]) { InterfaceDemo obj = new InterfaceDemo(); obj.print(); } }

Trong ví dụ trên, interface Printable và Showable có cùng các phương thức print() nhưng trình triển khai của nó được cung cấp bởi lớp InterfaceDemo, vì thế không có tính lưỡng nghĩa ở đây.

Ví dụ đa thừa kế với class

public class Printable { void print() { System.out.println("Printable"); } } public class Showable { void print() { System.out.println("Showable"); } } // Không thể thực hiện đa thừa kế với class public class InterfaceDemo extends Printable, Showable { public static void main(String args[]) { InterfaceDemo obj = new InterfaceDemo(); obj.print(); // Không thể xác định được gọi phương thức print() của class nào } }

Trong ví dụ trên, lớp Printable và Showable có cùng các phương thức print() và InterfaceDemo kế thừa 2 class đó không override lại phương thức print() nên trình biên dịch không biết thực thi phương thức print() của lớp Printable hay là của lớp Showable. Để bảo đảm an toàn và giảm tính phức tạp của hệ thống nên Java không hỗ trợ đa thừa kế đối với class.

Từ khóa » Java đa Kế Thừa Hay đơn Kế Thừa