Object-oriented three characteristics: Inheritance encapsulation, inheritance, polymorphism of

Object-oriented three characteristics: encapsulation, inheritance, polymorphism.

inherit

Inheritance is a prerequisite for polymorphic, if there is no inheritance, no polymorphism.

The main problem to solve is inherited : common extraction.

Inherit the characteristics of the relationship among:

  1. Subclasses can have the "content" of the parent class
  2. Subclasses can also have their own proprietary content.

Inherited format:

In the inheritance relationship, "sub-class is a parent class." That is, the subclass can be viewed as a parent.
Cases parent is an employee, a subclass is a lecturer, then the "instructor is an employee." Relationship: is-a.

Format defined parent class :( define a general class)
 public  class the parent class name {
     // ... 
} 

subclassing format: 
public  class subclass name extends the parent class name {
     // ... 
}

 

Three methods of the same name class region of the molecule:

  • Local variables: write directly to member variables
  • This class member variables:. This member variables
  • The parent class member variables:. Super member variables

Note:
Whether it is a member variable or member method, if not all looking up the parent class, subclass will not find down .

  • Rewrite (Override): the same method name, parameter list [too]. Cover, overwritten.
  • Overload (Overload): the same method name: argument list [] is not the same.

Methods overwritten features: create a subclass object, the priority sub-class method.

Note rewritten method covering:

1. The name must use the same method between the parent-child class, the parameter list is also the same.

  • @Override: written before the method for detecting a valid right is not overwritten.
  • This comment does not even write, as long as meet the requirements, but also the right approach overwritten.

2. The method return value must subclass [less] parent class method returns the value range.

  • Small extension Note: java.Lang.Object class is the highest of all classes of common parent (ancestor class), java.lang.String is a subclass of Object.

3. The method of subclass permission must be greater than or equal] [parent process permission modifier.

  • Small extension Note: public> protected> (default)> private
  • Note: (default) is not a keyword default, but nothing to write, leave it blank.

Use super and this keyword

Expansion:
The subclass must call the constructor of the parent class, do not write the gift super (); wrote the call with a specified super writing, only one super, it must also be the first.

super keyword is used to access class content of the parent class, and this keyword is used to access the contents of the parent class. Usage also has three:

  1. In the class member method, access the original member variables.
  2. In the method of the present class member, another member of this class access method.
  3. In the present method, the constructor, another configuration access to the class method.

In the third usage to note:

  • A: this (...) call must be the first statement in the constructor, the only one.
  • B: super structure and this call two, not both.

Guess you like

Origin www.cnblogs.com/cainiao-chuanqi/p/11110165.html