Java Essay: Super keyword

Source: Internet
Author: User
Tags class manager

Super keyword

First, Concept introduction.

1. If you work in a company, there are some differences in the treatment of managers in the company with those of ordinary employees. However, there are many similarities between them, for example, they can get paid. It's just that the average employee gets paid only after the job is done, and the manager receives the bonus after the expected performance. This information is represented in part of the code below.

1  Public classManagerextendsemployee{2     //This adds a field for storing bonus information, and a new way to set up the domain3     Private Doublebonus;4      Public voidSetbonus (Doublebonus) {5          This. bonus=bonus;6     }7}

2. Override method.

  Some methods in the superclass do not necessarily apply to class manager. The Getsalary method in the manager class should return the sum of the salaries and bonuses. To do this, you need to provide a new way to override this method in the parent class:

1  Public class extends employee{2     // ****** 3      Public Double getsalary () {4     // ******     5     }6 }

If you want to implement this method it seems as long as you return the sum of the salary and bonus fields:

 1  public  class  Manager extends   employee{ 2  // ******  3  public  double    Getsalary () { 4  return  salary+bonus;  5  }  6  }

However, this method does not work. Because the Getsalary method of the manager class does not have direct access to the private domain of the superclass. That is, although each manager object has a domain named salary, the salary domain cannot be accessed directly in the Getsalary method of the manager class. Only methods of the employee class can access the private part. If the method of the manager class must access the private domain, it is necessary to use the interface with the public, the common method in the Employee class getsalary is such an interface.

1  Public class extends employee{2     // ****** 3      Public Double getsalary () {4         double basesalary=getsalary (); 5         return basesalary+bonus; 6     }7 }

But such code would cause an infinite number of calls to itself, knowing that the program crashed.

Summary: We want to invoke the Getsalary method in the superclass employee, not this method of the current class. To do this, you can use the specific keyword super to solve the problem.

Second, super function simple introduction.

1.super () calls the constructor method of the parent class from the subclass.

2. Access the method or property of the parent class that is masked in the subclass.

Combined with the Getsalary method in the 1th employee class. The following is the correct writing format for the Getsalary method in the manager class:

 1  public  class  Manager extends   employee{ 2  // ******  3  public  double    Getsalary () { 4  double  basesalary=super   5  return  basesalary+bonus;  6  }  7  }

  Subclasses can increase the method of reducing or overriding the superclass, but you must never delete any of the inherited fields and methods.

Please allow me to pull back the constructor again.

1  Public classManagerextendsemployee{2     //******3     Private Doublebonus;4      Public Doublegetsalary () {5         Doublebasesalary=Super. Getsalary ();6         returnbasesalary+bonus;7     }8      PublicManager (String name,DoubleSalaryintYearintMonthintDay ) {9         Super(name,salary,year,month,day);TenBonus=0; One     } A}

Because the manager class's constructor does not have access to the private domain of the employee class, it must be initialized with the constructor of the employee class, and we can implement a call to the superclass through super. The statement that uses super to call the constructor must be the first statement of the subclass constructor.

  If the constructor of a subclass is not shown to call the constructor of the superclass, the constructor of the superclass default (without parameters) is automatically called. If the superclass does not have a constructor that does not have a parameter, and the superclass is not called again in the constructor of the subclass, the Java compilation will error.

Iv. comparison of Super and this.

  Some people think that super and this reference are similar concepts, in fact, it is not appropriate to compare. This is because super is not a reference to an object, and you cannot assign super to another object variable, he is just a special keyword that instructs the compiler to call the superclass method.

The keyword this has two uses: one is to invoke the implicit argument, and the other is to call the other constructor of the class (like this , "This (" John Doe ", 18)").

The keyword Super also has two uses: the first is to call the superclass method, and the second is to call the superclass constructor.

These two keywords are used in a similar way when the constructor is called. the statement that invokes the constructor can only appear as the first statement of another constructor. Construction parameters can be passed to other constructors of this class, or to constructors of super-classes.

Five, pay attention to the point.

1.super () calls the constructor of the parent class from the subclass, and this () invokes other constructor methods within the same class.

2.this () and super () cannot appear in a constructor at the same time.

The 3.super () and this () must be placed in the first row within the construction method.

4.this () and super () all refer to objects, so they cannot be used in a static environment.

Java essay: Super 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.