A brief talk on Java's primary understanding of inheritance _java

Source: Internet
Author: User
Tags access properties inheritance

concept: inheritance means that the definition of a class can be reused for the code of the parent class based on another existing class, the subclass inheriting the parent class. Two classes of relationships: The parent class generally has the characteristics of the common features of each subclass, and subclasses can add some more personalized methods. The inheritance of a class is transitive, that is, subclasses can continue to derive subclasses, the class concept at the top level is more abstract, and the concept of classes at the lower level is more specific.

1. Define subclasses:

Syntax format

[modifier] class subclass name extends parent class name {

Sub-class body
}

Modifiers: Public Private protected default

Subclasses are new, unique content that subclasses add to the content of the parent class, including member variables, member methods, classes, interfaces, constructor methods, and so on.

Take a chestnut. In a company, an employee is a staff member employed by a company, and a manager is a special employee who manages a company, a special employee who has not only the attributes and methods of ordinary employees, but 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 number private double Salary Pay 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;
 The public void setId (int id) {this.id = ID;
 Public double getsalary () {return salary;
 The 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, payroll, 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 special allowance for its own attributes.

2. Accessibility attributes of subclasses to parent class members

Subclasses can inherit members of the parent class, but access to the parent class member is controlled by the access attribute.

Parent class and subclass in a package: private is not directly accessible, but we can get private members of the parent class through a member method with public access properties.

The parent class is not in the same package as the subclass: private and default cannot be accessed directly, but we can get private members of the parent class by means of a member method with public and protected access properties.

3. Overloading and overwriting of class member methods

When the name of a new member variable defined in a subclass is the same as the name of a member variable in the parent class, the subclass hides the corresponding member variable in the parent class.

The name of a member method defined in a subclass is an overload or overlay of the member method, in conjunction with the name of a member method in the parent class.

(1) Overloading of member methods

In front of the employee and manager Pest, 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 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 the member method

(2) Coverage of member methods

There are usually two forms of:

① in the member method defined by the subclass, first call the overridden member method in the parent class, and then add some action statements.

② in a member method defined by a subclass, the member method overridden by the parent class is not invoked, but a statement group is written again. This implements a full overwrite of the parent class. This method should be implemented when an operation of a subclass is completely different from the parent object operation.

Chestnuts:

There is a member method equals () in the object class that determines whether two objects are equal, and the code is:

public boolean euqals (Object obj) {return
  (this = = obj);
 }

As you can see, this member method compares whether two objects refer to an object at the same time.

But we now want to be able to implement a function that compares the content of two objects of the same type with equality. So we have a plural class in the following design, each of which consists of a real and imaginary part. The design feature 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 to 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 +")); }
 }
}

Results for (2.0 + 3.0i) <> (2.0-3.4i)

The above is a small series for everyone to talk about the inheritance of the elementary Java understanding of the entire content, I hope that we support cloud-Habitat Community ~

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.