Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 77 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
77
Dung lượng
461,5 KB
Nội dung
1 Chapter 4. OOPApplication ITSS Java Programming CAO Tuan-Dung, HUT 2 Inheritance Mechanism to create new classes using existing classes The existing class is called the parent class, or superclass , or base class The derived class is called the child class or subclass As the name implies, the child inherits characteristics of the parent That is, the child class inherits the methods and data defined by the parent class 3 Benefit of Inheritance Reusability Once a behavior (method) is defined in a super class, that behavior is automatically inherited by all subclasses Thus, you write a method only once and it can be used by all subclasses. Once a set of properties (fields) are defined in a super class, the same set of properties are inherited by all subclasses A class and its children share common set of properties A subclass only needs to implement the differences between itself and the parent. 4 Inheritance Superclass Account Superclass Account Sub-class IntegratedAccount Basic function of Account + Class that defines original behavior Class that defines basic function of Account All the behaviors of superclass(attribute, operation, relation) are inherited to the sub-class 5 Definition of sub-class To derive a child class, we use the extends keyword. Syntax: [Modifier] class Sub-class_name extends SuperClass_name{ Definition of member variable to add Method definition to add or redefine } // Account class (superclass) class Account { String name; //Account name int balance; //Balance void display(){… } // Display } // Integrated account class (sub-class) class IntegratedAccount extends Account { int limitOverdraft; //Borrowing amount limit int overdraft; //Borrowing amount void loan(int money){…} //Borrowing() void display(){… } // Display } IntegratedAccount obj= new IntegratedAccount(); 6 Another example public class Person { protected String name; protected String address; /** * Default constructor */ public Person(){ System.out.println(“Inside Person:Constructor”); name = ""; address = ""; } . . . . } 7 Object of sub-class name balance display(){…} superclass part object of sub-class limitOverdraft overdraft display2(){…} loan(){…} In the object of the sub- class, there are the member variable and the method that were defined by the superclass, and the member variable and the method that were added by the sub-class. 8 Example: Inheritance.java // Account class class Account { // Member variable private String name; // Account name private int balance; // Balance // Value setting Method public void setData(String pName, int pInitialBalance) { name = pName; balance = pInitialBalance; } // Value setting Method public void display() { System.out.print("Account name : " + name); System.out.println(“\tBalance : " + balance + “VND"); } } // Integrated account class class IntegratedAccount extends Account { // Additional member variable private int limitOverdraft; // Borrowing amount limit private int overdraft; // Borrowing amount // Additional method // // Setting of borrowing amount limit Method public void setLimitOverdraft(int pLimitOverdraft) { limitOverdraft = pLimitOverdraft; overdraft = 0; } public void loan(int pOverdraft) { if((overdraft+pOverdraft) <= limitOverdraft) { overdraft += pOverdraft; } else { System.out.println(“Total of borrowing exceeds borrowing amount limit "); } } 9 Inheritance.java // Display of value 2 Method public void display2() { System.out.println(“\t\t Borrowing amount limit : "+ limitOverdraft + “VND"); System.out.println(“\t\t Borrowing amount : " + overdraft + “VND"); } } // Application class Exercise of inheritance class Inheritance { public static void main(String args[]) { // Generation of sub-class’s object IntegratedAccount obj = new IntegratedAccount(); // Method call of superclass (Message transmission) obj.setData(“Duc Binh", 100000); obj.display(); // Method call of sub-class (Message transmission) obj.setLimitOverdraft(500000); obj.loan(200000); obj.display2(); } } 10 Override Redefine by the subclass the method is defined by the super class. A child class can override the definition of an inherited method in favor of its own The new method must have the same signature as the parent's method, but can have a different body The type of the object executing the method determines which version of the method is invoked [...]... { super.display(); // Call of superclass method System.out.println(“Borrowing amount limit" + limitOverdraft + "Yen"); System.out.println(" Borrowing amount" + overdraft + "Yen"); } } 21 ThisSuper .java // Application class ThisSuper class ThisSuper { public static void main(String args[]) { // For constructor without argument, call constructor with argument g, g Account obj = new Account(); obj.display();... void loan(int pOverdraft) { if((overdraft+pOverdraft) . 1 Chapter 4. OOP Application ITSS Java Programming CAO Tuan-Dung, HUT 2 Inheritance Mechanism to create new classes. the member variable and the method that were added by the sub-class. 8 Example: Inheritance .java // Account class class Account { // Member variable private String name; // Account name private. System.out.println(“Total of borrowing exceeds borrowing amount limit "); } } 9 Inheritance .java // Display of value 2 Method public void display2() { System.out.println(“ Borrowing amount