Concept: Inheritance means that the definition of a class can be based on another existing class, that is, the subclass inherits the parent class, thereby realizing the reuse of the parent class's code. The relationship between two classes: the parent class generally has the characteristics of common to each subclass, while the subclass can add some more personalized methods. Class inheritance is transitive, that is, subclasses can continue to derive subclasses, the class concept located at the upper level is more abstract, and the concept of classes located at the lower level is more concrete.
1. Define subclasses:
Syntax format
[Modifier] class subclass name extends parent class name {
Subclass
}
Modifier: public private protected default
A subclass body is a new unique content added by a subclass based on inheriting the content of the parent class. It can include member variables, member methods, classes, interfaces, construction methods, etc.
For example, in a company, an employee is a staff hired by the company, and a manager is a special employee in the management company. This type of special employee not only has the attributes and methods of ordinary employees, but also some attributes and methods of their own, such as special allowances.
The code is as follows:
public class EmployeeClass{ private String name; // Name private int id; // Company number 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 code for the employee class, which has four attributes, name, number, salary, 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() + "/specialsal:" +specialsalary; } }This is a subclass, manager class, with its own attributes, special allowances.
2. Subclass accessibility to parent class members
A subclass can inherit the members of the parent class, but access to the members of the parent class is controlled by the access feature.
The parent class and the child class are in the same package: private cannot be accessed directly, but we can get the private member of the parent class through a member method with a public access attribute.
The parent class and the child class are not in the same package: private and defaults cannot be accessed directly, but we can get the private member of the parent class through the member method with public and protected access properties.
3. Overloading and overwriting of class member methods
When the name of the new member variable defined in the subclass is the same as a member variable in the parent class, the subclass hides the corresponding member variable in the parent class.
An overload or override of a member method is a member method defined in a subclass when the name of a member method defined in the parent class is the same as the name of a member method in the parent class.
(1) Overloading of member methods
In the employee and manager chestnut mentioned earlier, 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); }In the manager class, it can be defined as:
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 member methods
(2) Override of member methods
There are usually two forms:
① In the member method defined by the subclass, first call the overridden member method in the parent class, and then add some operation statements.
② In the member methods defined by the subclass, the member methods overridden by the parent class are not called, but a statement group is rewrite. This allows full coverage of the parent class. This method should be implemented when an operation of a subclass is completely different from the operation of the parent class object.
chestnut:
In the object class, there is a member method equals() that determines whether two objects are equal, and its code is:
public boolean euqals(Object obj){ return (this == obj); }It can be seen that this member method is to compare whether two objects refer to one object at the same time.
But we now hope to implement a function that compares whether the contents of two objects of the same type are equal. So we have a plural class below, each plural class consists of a real part and an imaginary part. The design function can compare whether two complex 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 +")" ); } }}The result is (2.0 + 3.0i) <> (2.0-3.4i)
The above is the brief discussion of Java's primary understanding of inheritance that the editor brings to you. I hope everyone will support Wulin.com more~