Bài giảng Lập trình hướng đối tượng - Chương 3: Thừa kế và đa hình - Trần Công Án

Nội dung

• Thừa kế

 Thừa kế là gì?

 Thừa kế trong Java

 Hàm xây dựng trong thừa kế

• Đa hình

 Nạp đè phương thức

 Đa hình

 Ứng dụng của tính đa hình

• Lớp trừu tượng & Phương thức trừu tượng

• Đa thừa kế (multiple inheritance)

• Giao diện (interface)

pdf67 trang | Chia sẻ: phuongt97 | Lượt xem: 344 | Lượt tải: 0download
Bạn đang xem trước 20 trang nội dung tài liệu Bài giảng Lập trình hướng đối tượng - Chương 3: Thừa kế và đa hình - Trần Công Án, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
ả lập đa thừa kế  Đa thừa kế (multiple inheritance) public class ChildClass implements InterfaceOne, InterfaceTwo { private InterfaceOne one; private InterfaceTwo two; ChildClass(InterfaceOne one, InterfaceTwo two) { this.one = one; this.two = two; } @Override public String methodA(int a) { return one.methodA(a); } @Override public void methodB(String s) { two.methodB(s); } } CT176 – Lập trình Hướng đối tượng 50 Giao diện (interface) CT176 – Lập trình Hướng đối tượng 51 Giao diện (interface) • Một giao diện có thể xem là một lớp hoàn toàn ảo: tất cả các phương thức đều không được cài đặt • Một giao diện:  Chỉ chứa các khai báo của các phương thức với thuộc tính truy cập public  Hoặc các hằng số tĩnh public (public static final...)  Được khai báo bằng từ khóa interface  Giao diện (interface) [public] interface [extends ] { //khai báo của các hằng số (static final ...) //khai báo các phương thức } CT176 – Lập trình Hướng đối tượng 52 Giao diện (interface) • Giao diện đóng vai trò như một “cam kết” (contract):  Giao diện có thể làm được gì (nhưng không chỉ định làm như thế nào)  Qui ước đặt tên: tiếp vị ngữ -able (có khả năng/có thể) • Một lớp có thể cài đặt (implement) các giao diện:  Cài đặt tất cả các phương thức của các giao diện  Xác nhận khả năng của lớp có thể làm được gì  Sử dụng từ khóa implements • Không thể tạo đối tượng thuộc một giao diện, nhưng có thể tạo tham chiếu thuộc kiểu giao diện  Giao diện (interface) CT176 – Lập trình Hướng đối tượng 53 Ví dụ  Giao diện (interface) public interface Movable { //abstract methods to be implemented //by the subclasses public void moveUp(); public void moveDown(); public void moveLeft(); public void moveRight(); } CT176 – Lập trình Hướng đối tượng 54 Ví dụ  Giao diện (interface) public class MovablePoint implements Movable { private int x, y; //coordinates of the point public MovablePoint(int x, int y) { this.x = x; this.y = y; } public String toString() { return "(" + x + "," + y + ")"; } public void moveUp() { y--; } public void moveDown() { y++; } public void moveLeft() { x--; } public void moveRight() { x++; } } public class TestMovable { public static void main(String[] args) { Movable m1 = new MovablePoint(5, 5); System.out.println(m1); m1.moveDown(); System.out.println(m1); m1.moveRight(); System.out.println(m1); } } Kết quả: CT176 – Lập trình Hướng đối tượng 55 Phương thức mặc định (default method) • Từ Java 8, các giao diện có thể có các phương thức mặc định:  Là phương thức được cài đặt (có thân hàm)  Cho phép thêm vào giao diện các phương thức mà không làm các lớp đã cài đặt giao diện bị lỗi • Các lớp cài đặt phương thức có thể cài đặt hay không cài đặt các phương thức mặc định:  Không cài đặt: thừa kế phương thức mặc định  Cài đặt: nạp đè phương thức mặc định của giao diện  Không cài đặt, chỉ khai báo: phương thức ảo. • Cú pháp: thêm từ khóa default trước khai báo  Giao diện (interface) CT176 – Lập trình Hướng đối tượng 56 Tổng kết • Tính thừa kế cho phép sử dụng lại mã (reuse code) • Lớp thừa kế được gọi là lớp con, lớp được thừa kế được gọi là lớp cha • Lớp con có tất cả các thành phần của lớp cha  Định nghĩa thêm thuộc tính hoặc phương thức mới  Nạp đè (overriding) hàm của lớp cha • Quan hệ giữa lớp con và lớp cha là quan hệ là (is-a) • Tính đa hình cho phép các loại đối tượng khác nhau ứng xử khác nhau với cùng 1 thông điệp • Đa hình: thừa kế + nạp đè hàm + liên kết động  Tổng kết CT176 – Lập trình Hướng đối tượng 57 Tổng kết • Java không hỗ trợ đa thừa kế (lớp con có hơn 1 lớp cha) • Các kỹ thuật mô phỏng đa thừa kế:  Hàm mặc nhiên (default method) của giao diện  Quan hệ composition (hay delegation) • Giao diện:  Đóng vai trò như một “cam kết” về tính năng của một kiểu  Như là một lớp hoàn toàn ảo: chỉ có khai báo phương thức, không có định nghĩa phương thức và các thuộc tính  Tổng kết CT176 – Lập trình Hướng đối tượng 58 Tổng kết  Tổng kết https://goo.gl/iK0Bj2 CT176 – Lập trình Hướng đối tượng 59 Quiz 1. Which of the following is true about inheritance in Java? a) Private methods are final. b) Protected members are accessible within a package and inherited classes outside the package. c) Protected methods are final. d) We cannot override private methods. e) In Java all classes inherit from the Object class directly or indirectly, the Object class is root of all classes. f) Multiple inheritance is not allowed in Java  Quiz CT176 – Lập trình Hướng đối tượng 60 Quiz 2. Say that there are three classes: Computer, AppleComputer, and IBMComputer. What are the likely relationships between these classes? a) Computer is the superclass, AppleComputer and IBMComputer are subclasses of Computer b) IBMComputer is the superclass, AppleComputer and Computer are subclasses of IBMCompute c) Computer, AppleComputer and IBMComputer are sibling classes d) Computer is a superclass, AppleComputer is a subclasses of Computer, and IBMComputer is a sublclas of AppleComputer  Quiz CT176 – Lập trình Hướng đối tượng 61 Quiz 3. Can an object be a subclass of another object? a) Yes, as long as single inheritance is followed b) No, inheritance is only between classes c) Only when one has been defined in terms of the other d) Yes, when one object is used in the constructor of another. 4. How many objects of a given class can there be in a program? a) One per defined class b) One per constructor definition c) As many as the program needs d) One per main() method  Quiz CT176 – Lập trình Hướng đối tượng 62 Quiz 5. Which of the following is correct syntax for defining a new class Jolt based on the superclass SoftDrink? a) class Coca isa SoftDrink { /*class member */ } b) class Coca implements SoftDrink { /*class member */ } c) class Coca defines SoftDrink { /*class member */ } d) class Coca extends SoftDrink { /*class member */ } 6. What restriction of using the super in a constructor? a) It can only be used in the parent's constructor b) Only one child class can use it c) It must be used in the last statement of the constructor d) It must be used in the first statement of the constructor  Quiz CT176 – Lập trình Hướng đối tượng 63 Quiz 7. Output of following Java Program?  Quiz class Base { public void show() { System.out.println("Base::show() called"); } } class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } } public class Main { public static void main(String[] args) { Base b = new Derived();; b.show(); } } a) Derived::show() called b) Base::show() called CT176 – Lập trình Hướng đối tượng 64 Quiz 8. Output of following Java Program?  Quiz class Base { final public void show() { System.out.println("Base::show() called"); } } class Derived extends Base { public void show() { System.out.println("Derived::show() called"); } } class Main { public static void main(String[] args) { Base b = new Derived();; b.show(); } } a) Base::show() called b) Derived::show() called c) Compiler Error d) Runtime Error CT176 – Lập trình Hướng đối tượng 65 Quiz 9. Output of following Java Program?  Quiz class Base { public static void show() { System.out.println("Base::show() called"); } } class Derived extends Base { public static void show() { System.out.println("Derived::show() called"); } } class Main { public static void main(String[] args) { Base b = new Derived();; b.show(); } } (A) Base::show() called (B) Derived::show() called (C) Compiler Error CT176 – Lập trình Hướng đối tượng 66 Quiz 10. Output of following Java Program?  Quiz (A) Base Derived Derived (B) Base Base Derived (C) Base Derived Base (D) Complier Error class Main { public static void doPrint(Base b) { b.print(); } public static void main(String[] args) { Base x = new Base(); Base y = new Derived(); Derived z = new Derived(); doPrint(x); doPrint(y); doPrint(z); } } class Base { public void print() { System.out.println("Base"); } } class Derived extends Base { public void print() { System.out.println("Derived"); } } CT176 – Lập trình Hướng đối tượng 67 Quiz 11. Output of following Java Program?  Quiz class Grandparent { public void print() { System.out.println("Grandparent's print()"); } } class Parent extends Grandparent { public void print() { System.out.println("Parent's print()"); } } class Child extends Parent { public void print() { super.super.print(); System.out.println("Child's print()"); } } public class Main { public static void main( String[] args) { Child c = new Child(); c.print(); } } CT176 – LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG Question?

Các file đính kèm theo tài liệu này:

  • pdfbai_giang_lap_trinh_huong_doi_tuong_chuong_3_thua_ke_va_da_h.pdf