Inheritance relationship
There are three relationships between the two classes:
- Dependency, uses-A. If a class method is used to manipulate another object, we will say that one class depends on another class.
- Aggregation (association), has-a, an object contains another object. Aggregation means that the object of Class A contains the object of Class B.
- Inheritance: Is-A. If there is an obvious is-a (yes) Relationship between the two classes, for example, if every manager is an employee, the two classes have an inheritance relationship.
For example:
class Manager extends Employee{ ......}The manager inherits the employee class and can override the superclass method or add a method. That is, the subclass has more functions than the superclass. Method rewriting when the subclass overrides the superclass method, you can also call the method with the same name as the superclass, just use super. Method (). Note that the method to be rewritten cannot be lower than the access permission of the superclass method! For example:
// Override the getsalary method public double getsalary () {double basesalary = super. getsalary (); // call the getsalary method return basesalary + bonus ;}
The subclass overrides the getsalary () method and also calls the method with the same name as the superclass.
In addition, the super () method can also be used in the constructor to call the constructor method of the super class. Note that the super () method must be the first statement of the subclass constructor. Otherwise, the compiler will give a constructor call must be the first statement in a constructor error reminder. For more information, see the final example. Polymorphism and dynamic binding of an object (for example, e in the following example) can reference a variety of actual types of phenomena called polymorphism. At runtime, You can automatically choose to call that method and think of it as dynamic binding. Java does not support multiple inheritance, but only supports single inheritance. In the following example, e of the emplyee type can reference objects of the emplyee type, Manager type, and boss type. when getsalary () is used, the compiler knows the actual type of E to accurately call the getsalary method of the actual type class. If it does not exist, it calls the method of its superclass, this is dynamic binding. Final blocks inheritance.
class Employee{......public final String getName(){return name;}......}final class Manager extends Employee{......}
In the preceding example, the manager class cannot be inherited, And the getname method cannot be overwritten by the quilt class.
The reason for forced type conversion: after the actual type of the object is temporarily ignored, all functions of the object are used. Only the inheritance chain can be converted from top to bottom. For example, you can convert the actual type of staff [1] Which is the employee type of manager to the Manager type.
Manager man = (Manager)staff[1];
A summary of the examples is as follows:
Package COM. xujin; public class test {public static void main (string [] ARGs) {employee [] Staff = new employee [3]; staff [0] = new employee ("Bob ", 1000); staff [1] = new manager ("Jim", 5000,100 0); staff [2] = new boss ("Alice", 7000,100 0, 10000 ); for (employee E: Staff) system. out. println ("Class Name:" + E. getclass (). getname () + "\ TID:" + E. GETID () + "\ tname:" + E. getname () + "\ tsalary:" + E. getsalary (); Manager man = (manager) staff [1]; boss = (boss) staff [2]; system. out. println (man. getbonus (); // After type conversion, you can use all functions of the actual type system. out. println (boss. getaward (); // classcastexception exception. Conversion from top to bottom on the inheritance chain is not allowed. // boss myboss = (boss) staff [0]; // combine the instaceof operator and type conversion to avoid exceptions if (staff [0] instanceof boss) {system. out. println ("staff [0] is a instace of Boss"); Boss myboss = (boss) staff [0];} else system. out. println ("staff [0] isn' t a instace of Boss"); If (staff [2] instanceof boss) {system. out. println ("staff [2] is a instace of Boss");} else system. out. println ("staff [2] isn't a instace of Boss");} class employee {public employee (string name) {This. name = Name; id = nextid; nextid ++;} public employee (string name, double salary) {This (name); // call another constructor this. salary = salary;} // defines the accessors method public final string getname () {return name;} public double getsalary () {return salary;} public final int GETID () {return ID;} // defines the modifier method public final void setname (string name) {This. name = Name;} public final void setsalary (double salary) {This. salary = salary;} public final void raisesalary (double percent) {This. salary * = (1 + percent);} // defines the variable private string name = ""; // instance domain initialization private double salary; private int ID; private Static int nextid = 1;} class manager extends employee {Public Manager (string name, double salary, double bonus) {super (name, salary ); // super is used in the constructor. You can call the constructor setbonus (bonus);} public double getbonus () {return bonus ;} // override the getsalary method public double getsalary () {double basesalary = super. getsalary (); // call the getsalary method of the superclass return basesalary + bonus;} public void setbonus (double bonus) {This. bonus = bonus;} private double bonus;} final class boss extends manager {public boss (string name, double salary, double bonus, Double Award) {super (name, salary, bonus); this. award = Award;} // rewrite the getsalary method public double getsalary () {double basesalary = super. getsalary (); // call the getsalary method of the superclass return basesalary + Award;} public double getaward () {return Award;} private Double Award ;}The abstract class finally describes the concept of the abstract class. An abstract class is an ancestor class dedicated for extension. An abstract class itself cannot define an object of this class, that is, an abstract class cannot be instantiated.
Person p = new Student("joun", 17, 6000);
Here, P is a variable of the person type, but it references the student type instance.
There are two methods in the abstract class. One is normal, the same as the method in the general class, and the other is abstract, which plays a placeholder role, in the future, subclass inheritance will implement this method. Classes can be declared as abstract methods even if they do not contain abstract methods.
Abstract class abstract methods can be used to implement polymorphism. For example, in the following example, A getsubstring () method is defined in the abstract class, which is implemented by two subclasses: employee and student, however, different implementation methods are used. When the getask() method is called by P of the person type, the determined method is called based on the actual type of P.
Package COM. xujin; public class test {public static void main (string [] ARGs) {person [] People = new person [2]; people [0] = new employee ("bod ", 34,500 0); People [1] = new student ("Joun", 17,600 0); For (person P: People) {system. out. print ("name:" + P. getname () + "\ Tage:" + P. getage () + "\ tdescription:" + P. getdescription () + "\ t"); If (P instanceof employee) {system. out. println (employee) P ). getask();} else if (P instanceof student) system. out. println (student) P ). getask() ;}}abstract class person {public person (string name, int age) {This. name = Name; this. age = age;} public abstract string getdescription (); Public final string getname () {return this. name;} public final void setname (string name) {This. name = Name;} public final int getage () {return this. age;} public final void setage (INT age) {This. age = age;} private string name; private int age;} class employee extends person {public employee (string name, int age, double employee) {super (name, age ); id = nextid; nextid ++; this. operator = Operator;} // defines the abstract method of the person abstract class public string getdescription () {return "this is an employee. class Name: "+ this. getclass (). getname () ;}// defines the accessors method public double getiterator () {return iterator * 2;} public final int GETID () {return ID ;} // define the modifier method public final void setary (double salary) {This. attributes = attributes;} // defines the variable private double attributes; private int ID; Private Static int nextid = 1;} class student extends person {public student (string name, int age, double keys) {super (name, age); this. attributes = attributes;} Public String getdescription () {return "this is a student. class Name: "+ this. getclass (). getname ();} public double getask() {return this. required;} public void setparts (Double Partial) {This. rows = rows;} private double rows ;}
Result: