tailieunhanh - Java C4. OOP Application

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. Once a behavior (method) is defined in a super class, that behavior is automatically inherited by all subclasses | Chapter 4. OOP Application ITSS Java Programming CAO Tuan-Dung, HUT 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 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. Inheritance Superclass Account Superclass Account Sub-class IntegratedAccount Basic . | Chapter 4. OOP Application ITSS Java Programming CAO Tuan-Dung, HUT 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 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. 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 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(); Another example public class Person { protected String name; protected String address; /** * Default constructor */ public Person(){ (“Inside .

TỪ KHÓA LIÊN QUAN