Consolidate java -- super and super ()

Source: Internet
Author: User
Tags class manager

Consolidate java -- super and super ()
Introduction:

There are common employees and managers in a company. There are many similarities between them, but there are also some differences, such as salary problems. ordinary employees only have regular salaries, the manager has a certain percentage of bonuses after completing the performance. In this case, we can define two classes: Employee and Manager. Obviously, there is an obvious "is-a" relationship between the two classes. The Manager is also an Employee of the company, the "is-a" relationship is a feature of the inheritance relationship, so there is an inheritance relationship between the Employee and the Manager.

 

Body:
The following is the Employee class:

/*** Employee class */public class Employee {private int no; // Employee ID private String name; // Employee name private double salary; // Employee salary public Employee () {} public Employee (int no, String name, double salary) {this. no = no; this. name = name; this. salary = salary;} public double getSalary () {return salary ;}}

If the Manager class is defined as follows, it is obviously incorrect:

 

 

/*** Manager class * error code */class Manager extends Employee {private double reward; // The reward public void Manager (int no, String name, double salary, double reward) {this. no = no; this. salary = salary; this. name = name; this. reward = reward ;}}

 

The Manager class inherits the Employee class, so it inherits the member variables of the Employee, but the Manager does not have the no, salary, and name attributes, while the three attributes of the Employee are private, it cannot be called by any class other than itself, even its subclass. What should I do? As follows:
Class Manager extends Employee {private double reward; // public Manager (int no, String name, double salary, double reward) {super (no, name, salary ); // this must be placed in the first line of the constructor. reward = reward ;}}

Super (no, name, salary) is short for calling the public Employee (int no, String name, double salary) of the Employee class.
It should be mentioned that if super () is written, the null constructor of its super class will be called, but if the constructor of its super class is not displayed in the subclass, the system still calls the null constructor of its superclass by default. If there is no null constructor of the superclass at this time, a compilation error is reported.

For the Manager, the salary is slalary + reward. Therefore, the getSalary () method needs to be rewritten in the Manager. first look at the following code:
// Error code public double getSalary () {return reward + salary ;}
This code is incorrect. Why? This is because subclass cannot directly call the private domain of its superclass. What if we use the getSalary () method of the superclass?

// Error code
Public double getSalary (){
Return reward + getSalary ();
}
Although this code does not report an error, it is also an error. Although the superclass has getSalary (), the subclass Manager also inherits the method, and the member variables and member methods in the subclass have a high priority, therefore, this method of subclass will be preferentially called, that is, the method being written, which will cause infinite recursive calls.
The correct method is to use the super keyword, as shown below:
Public double getSalary (){
Return reward + super. getSalary ();
}

 

 

Similarities and differences between super and this [Source: http://blog.csdn.net/anmei2010/article/details/4093118.pdf ]:
1) super (parameter): Call a constructor in the superclass (it should be the first statement in the constructor)

2) this (parameter): Calls another constructor in this class (it should be the first statement in the constructor)

3) super: it refers to the members of the direct parent class of the current object (used to access member data or functions of the hidden parent class in the direct parent class, when the base class and the derived class have the same member definition, such as: super. variable name super. member letter data name (real parameter)

4) this: it represents the name of the current object (this should be used to indicate the current object when it is prone to ambiguity in the program; if the member data in the function form participation class has the same name, this is required to specify the member variable name)

5) Calling super () must be written in the first line of the subclass constructor; otherwise, the compilation fails. The first statement of each subclass constructor method implicitly calls super (). If the parent class does not have such constructor, an error is reported during compilation.

6) super () is similar to this (). The difference is that super () calls the constructor of the parent class from the subclass. this () calls other methods in the same class.

7) both super () and this () must be placed in the first line of the constructor.

8) although you can use this to call a constructor, you cannot call two constructor.

9) this and super cannot appear in a constructor at the same time, because this will inevitably call other constructor, and other constructor will inevitably have super statements, therefore, if the same statement exists in the same constructor, the meaning of the statement is lost and the compiler will not pass.

10) this () and super () both refer to objects. Therefore, they cannot be used in the static environment. Including static variables, static methods, and static statement blocks.

11) in essence, this is a pointer to the current object, but super is a Java keyword.



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.