Java Basics Xi.

Source: Internet
Author: User
Tags control characters instance method

11th Chapter
    • Class inheritance

Subclasses inherit the data fields and methods that are accessible in the parent class, and subclasses can also add new data fields and methods.
Subclasses do not inherit the constructor of the parent class .
A subclass can have only one direct parent class: single inheritance .

    • Construction Order

When the child class object is created for the 1th time, the static member variable of its parent class is initialized first (if no parent class object is instantiated), and then the static member variable of the current subclass object is initialized.
Note: When you create a subclass object after the 1th time, the static member variables of the parent class and subclass are not initialized again. In fact, a static member variable already exists before any instance object .
The constructor super (...) that executes the parent class of the subclass object (if the parent class is not defined, it must be object), may be compiled as the default parameterless constructor super () provided by the parent class.
The initialization expression is then executed when the subclass instance member variable is defined, and the default is 0 or null if no initialization value is given.
Finally executes the subclass constructor in the body of super (...). The following statement .

    • Super keyword

Use supper to explicitly invoke the constructor of the parent class
Super (PARAMETERSOPT) invokes the constructor of the parent class.
must be 1th of the subclass constructor and only 1 statements (The parent class is constructed first).
If the constructor of the parent class is not explicitly called in the subclass constructor, the parent class constructor with no parameters is automatically called .

When you add an parameterless constructor to a subclass, the non-parametric constructor of the parent class is called by default with super (), and if the parent-class parameterless constructor is not found, the child class adds the parameterless constructor failure.

    • Method overrides

If a subclass redefined an instance method inherited from a parent class, it is called a method override.
Only when the method is an accessible instance method can it be overwritten, that is, the private instance method cannot be overwritten, and the private method is automatically considered final.
Static methods cannot be overwritten, and if a static method is redefined in a subclass, the parent class method is hidden.
Once a method in the parent class is overwritten, the overridden parent class method cannot be accessed through the referenced subclass object. You can use Super to refer to the overridden parent class method in the subclass class body function.
Overwrite: The subclass object is referenced by the parent class reference variable, and the parent class refers to the same signature function called by the variable as a subclass function (cannot discover the parent class function, late binding).

Hidden: Same as above, but parent class reference variable accesses parent class variable, function (can be discovered again).

    • Methods in the Object

Equals method: Used to test whether the properties of two objects are equal. The default implementation is to override the subclass by comparing whether two variables refer to the same object, rather than checking whether the object's properties are the same. For example, overwrite in the Circle class:
public boolean equals (Object o) {
if (o instanceof Circle)//should first check the type of another object o
Return radius== (Circle) o). radius;//basic type, = = Judgment
return false;
}

ToString Method: Returns the string representing the object. Its default implementation is to return the class name, @, and hashcode components.
Circle.tostring ();//[email protected]: The default implementation is small and should be overridden in subclasses.
GetClass method: Used for generics and reflection mechanism, is a final method, it cannot be overwritten.

    • Polymorphic

Polymorphic: When invoking an instance function using a reference variable, the corresponding instance method of that type is executed according to the type of the actual object being referenced, thus showing that the different behavior is called polymorphism. by inheriting an instance method that overrides the parent class, the polymorphism is implemented .

When invoking an instance method, the Java virtual machine dynamically determines which method is called, called dynamic binding, or is a polymorphic

A parent class variable refers to a subclass object and can be considered to be a child class object to be converted to a parent class.
The conversion from a subclass to a parent class is legal , called an implicit conversion.
Person P=new Manager ();//Convert Subclass object to parent class object
The parent class to the subclass must be explicitly converted.
Manager m = p; Compile error
Manager m = (manager) P;//ok, but no check before conversion
It is safer to check from a parent class to a subclass when it must be explicitly converted.
Manager m = null;
if (P instanceof Manager) m= (manager) p; Security: Pre-conversion check

Overloads occur at compile time (Compile-time), and the compiler finds the most appropriate method based on the method signature. Early binding
When polymorphic occurs at run time, the runtime JVM finds the most appropriate instance method based on the true type of the object referenced by the variable. Late binding

    • Access control characters and Finall

Final member Variable: constant, which can no longer be modified after initialization of the data.
Final method: The End method, subclasses cannot overwrite.
Private instance method: Automatically considered final, subclasses cannot overwrite (this seems problematic, subclasses are not visible, but subclasses can define their same functions)
Final class: The final class, cannot derive subclasses.
Final local variable: constant, which cannot be modified after initialization of the data.



Java Basics Xi.

Related Article

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.