Chapter 8th Inheritance of the Java Development Handbook learning process

Source: Internet
Author: User

The relationships between classes and classes in Java:

    • Use. That is, the method of Class A manipulates the members of Class B (the object).
    • Have. That is, Class A has member reference variables of type B.
    • Inherited.

Inheritance and concealment:

Inheritance is also a kind of access.

When a member variable is declared as the default type, the child class outside the package cannot inherit the member variable.

The difference between protected and the default type is that when a member variable is decorated as protected, if the class accessing the variable is outside the package, the variable can be accessed only through inheritance, otherwise it cannot be compiled by reference to the subclass type.

the hiding of member variables occurs when a child class has a member variable with the same name as its parent class. This means that when the member variable is called directly in a subclass, the child class is called instead of the parent class. Of course, the member variables of the parent class are actually inherited, but they are hidden. So, to access variables of the hidden parent class, you have to use the super keyword.

Super and this are the same predefined object references.

Object:

An object reference variable can reference an object instance of its own type or an object of its subclasses and indirect subclasses.

Class A inherits from Class B, so the object reference variable of Class A can reference the object of Class B, but even if the object reference to Class A refers to an object of Class B, when an object reference to Class A invokes a member variable or method of the same name in Class B, the system still uses the object reference of Class A instead of the object it points to. Otherwise, a forced type conversion is required.

forcing a type conversion is simply a reference type, and will not interfere with the object that really points to it. in this sentence, it can also lead to another meaning, that is, for the compilation, as long as the converted reference type and the converted target type is derived or derived from the relationship, the compilation is passed; at the same time, the compilation system does not know what the specific object the reference refers to, and it can only judge the conversion with no hope by reference type. at run time, you will know if you can make a reference to a forced type conversion.

A reference assignment can directly assign a subclass reference to a parent class reference, and if you want to assign a parent class reference to a child class reference, you must make a forced type conversion.

For reference comparisons,there are corresponding requirements in Java:

  • References of the same type can be compared;
  • Different types of references are to be compared, where the types have derived relationships;
  • The comparison of references is to compare whether two references point to the same object.

Override of Method:

When a reference to a parent class is directed to a subclass object, accessing the overridden method accesses the method in the redefined subclass (which differs from the property that accesses the Class).

To make a rewrite, the method names in the subclass must be identical to the argument list (that is, the signature is consistent), at which point the following rules are followed:

  • return type to be compatible;
  • The access level limit must not be narrower than the overridden method, and can be wider than the overridden method;
  • You cannot override a method that is represented as final.

To override a relationship with inheritance:

If the method of the parent class is private, it cannot be inherited by the quilt class, and since it cannot be inherited, it may be rewritten, not to mention the rules in the rewrite.

static methods can be inherited, but cannot be overridden:

  • Non-static methods will attempt to override static methods when compiling an error;
  • The hiding of static methods. Both the parent and child classes have static methods with the same signature, but the parent class reference is called a method of a subclass that is called by the child class reference, regardless of the object.
  • A method inherited from a parent class is called when a subclass reference is called to a method that does not have the subclass itself, and the parent class has and can inherit (that is, not private).

You can extend the functionality of overridden methods in a subclass by super referencing the method of the parent class when overriding the method of the parent class.

Hiding is called based on the type of reference (called static methods and member variables), and overrides are called based on the type of object.

All of this is based on a very important principle in object-oriented- substitution , that is, subclasses can replace parent classes.

Overloads of the method:

It is only in the same class that a method overload is used. Object reference parameters in overloading are reference-oriented types, regardless of the specific object to which they are directed.

Final and inherited:

The final class cannot be inherited, so the method of the class cannot be overridden, and the final method cannot be overridden and can only be inherited.

Abstract and Inheritance:

An abstract class can never be instantiated, and its main purpose is to inherit and extend.

Abstract methods are declared only by methods, without a method body (that is, without curly braces).

Abstract classes can have non-abstract methods, this method is often the abstract class all future subclasses have, and do not because of the different subclasses to implement different methods.

Chapter 8th Inheritance of the Java Development Handbook learning process

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.