Java's primary understanding of inheritance, java inheritance
Concept: inheritance means that the definition of a class can inherit the parent class based on another existing class, that is, subclass, so as to reuse the code of the parent class. The relationship between the two classes: the parent class generally has the characteristics of the sub-classes, and the sub-classes can add more personalized methods. The inheritance of a class is passed, that is, the subclass can continue to derive the subclass. The class concept at the upper layer is more abstract and the class concept at the lower layer is more specific.
1. Define subclass:
Syntax format
[Modifier] class subclass name extends parent class name {
Subclass body
}
Modifier: public private protected default
The subclass body is a new special content added by the subclass based on the content of the parent class. It can contain member variables, member methods, classes, interfaces, constructor methods, and so on.
For example, in a company, an employee is a company employee, and a manager is a special employee who manages the company. Such special employees not only possess the attributes and methods of common employees, there are also some attributes and methods of his own, such as special allowances.
The Code is as follows:
Public class EmployeeClass {private String name; // name private int id; // company id private double salary; // salary private String department; // department public EmployeeClass () {} public EmployeeClass (String name, int id, double salary, String department) {this. name = name; this. id = id; this. salary = salary; this. department = department;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getId () {return id;} public void setId (int id) {this. id = id;} public double getSalary () {return salary;} public void setSalary (double salary) {this. salary = salary;} public String getDepartment () {return department;} public void setDepartment (String department) {this. department = department ;}@ Override public String toString () {return "EmployeeClass [name =" + name + ", id =" + id + ", salary = "+ salary +", department = "+ department +"] ";}}
This is the employee class code, which has four attributes: name, number, salary, and department.
public class ManagerClass extends EmployeeClass{ private double specialsalary; public ManagerClass(){super();} public ManagerClass(String name,int id,double salary,String department,double specialsalary){ super(name,id,salary,department); this.specialsalary = specialsalary; } public double getSpecialsalary() { return specialsalary; } public void setSpecialsalary(double specialsalary) { this.specialsalary = specialsalary; } @Override public String toString() { return super.toString() + "\nspecialsal:" +specialsalary; } }
This is a subclass, manager class, with a property of its own, special allowance.
2. Accessability of subclass to parent class members
Subclass can inherit the members of the parent class, but access to the members of the parent class is controlled by the access feature.
Parent class and Child class are in a package: private Members cannot be directly accessed, but we can obtain private Members of the parent class through the member method with the public access attribute.
The parent class and subclass are not in the same package: private and default cannot be accessed directly, but we can obtain the private Members of the parent class through the member methods with public and protected access attributes.
3. overload and overwrite of class member Methods
When the names of the new member variables defined in the subclass are the same as those of a member variable in the parent class, the subclass hides the corresponding member variables in the parent class.
When the name of the member method defined in the subclass is the same as the name of a Member method in the parent class, the member method is overloaded or overwritten.
(1) overload of member Methods
In the example of employee and manager, we can define a member method in the employee class.
public void setInfo(String name,int id,double salary,String department){ this.name = new String(name); this.id = id; this.salary = salary; this.department = new String(department); }
The manager class can be defined:
public void setInfo(String name,int id,double salary,String department,double specialsalary){ super(name,id,salary,department); this.specialsalary = specialsalary; }
This is the overload of the member method.
(2) override of the member Method
There are usually two forms:
① In the member methods defined by the subclass, first call the override member methods in the parent class, and then add some operation statements.
② In the member methods defined by the subclass, a new statement group is written instead of calling the member methods covered by the parent class. This completely overwrites the parent class. This method should be used when an operation of a subclass is totally different from that of a parent class object.
Chestnuts:
In the object class, there is a member method equals () used to determine whether two objects are equal. The code is:
public boolean euqals(Object obj){ return (this == obj); }
We can see that this Member method is to compare whether two objects reference an object at the same time.
However, we want to implement the function of comparing the content of two objects of the same type. Therefore, the following design has a plural class. Each plural class consists of a real part and a virtual part. The design function can compare whether two plural numbers are equal. The Code is as follows:
public class ComplexNumber { private double re; private double im; public ComplexNumber(){re = 0.0;im = 0.0;} public ComplexNumber(double re,double im){ this.re = re; this.im = im; } public double getRe() { return re; } public void setRe(double re) { this.re = re; } public double getIm() { return im; } public void setIm(double im) { this.im = im; } public boolean equals(Object otherObject){ if(this == otherObject) return true; if(otherObject == null) return false; if(getClass() != otherObject.getClass()) return false; ComplexNumber other = (ComplexNumber)otherObject; if((re == other.re) && (im == other.im)) return true; else return false; } public String toString(){ String str = ""; if(re != 0) str += re; if(im == 0) return str; if( im < 0 ) str += im +"i"; else str += " + " + im +"i"; return str; } public static void main(String[] args) { ComplexNumber c1,c2; c1 = new ComplexNumber(2,3); c2 = new ComplexNumber(2,-3.4); if(c1.equals(c2)){ System.out.println("("+c1+") == ( " + c2 +")" ); } else{ System.out.println("("+c1+") <> ( " + c2 +")" ); } }}
Result: (2.0 + 3.0i) <> (2.0-3.4i)