The inheritance, overloading and rewriting of the basic knowledge--java class

Source: Internet
Author: User

Inherited

The extends keyword to represent an inheritance relationship. When a class is created, it is always inherited, and is always implicitly inherited from the root class object if it does not explicitly indicate the class to inherit;

Second, subclass inherits the member variable of the parent class

1) The ability to inherit the public and protected member variables of the parent class, and the private member variables of the parent class cannot be inherited;

2) for the package access member variable of the parent class, the subclass can inherit if the child class and parent class are under the same package, otherwise the subclass cannot inherit;

3) for the parent class member variable that the subclass can inherit, if a member variable of the same name appears in the subclass, a shadowing occurs, that is, the member variable of the child class masks the member variable with the same name as the parent class. If you want to access a member variable of the same name in the parent class in a subclass, you need to use the Super keyword to refer to it.

Iii. methods of inheriting the parent class by subclasses

1) The ability to inherit the public and protected member methods of the parent class, and the private member method of the parent class cannot be inherited;

2) for the parent class of the package access member method, if the child class and the parent class under the same package, the subclass can inherit, otherwise, the subclass can not inherit;

3) for the parent class member method that the subclass can inherit, if a member method of the same name appears in the subclass, then it is called overwrite, that is, the member method of the subclass overrides the member method with the same name as the parent class. If you want to access a member method of the same name in the parent class in a subclass, you need to use the Super keyword to refer to it.

Note: Hiding and overwriting are different. Shadowing is for member variables and static methods, and overrides are for common methods.

Four, the constructor

Subclasses are constructors that cannot inherit the parent class, but note that if the constructor of the parent class is parameterized, the constructor of the parent class must be called with the Super keyword in the constructor of the child class and be accompanied by the appropriate argument list. If the parent class has a parameterless constructor, it is not necessary to call the parent class constructor with the Super keyword in the subclass's constructor, and if the Super keyword is not used, the system automatically calls the parent class's parameterless constructor.

(The constructor invocation of the parent class and the initialization process must precede the subclass)

Overloads (Overload) and overrides (override)

First, overload

The method parameter number or type of the function is inconsistent, called the overload of the method.

In terms of meaning, only requiring the number of arguments or the type of argument is inconsistent, it is said that two functions are overloaded functions, and whether the return value is the same, it doesn't matter. Also, overloading can occur in the same class or in an inheritance relationship.

Second, rewrite

Rewriting is also known as overwrite. The override of a method must occur in two classes that have an inheritance relationship , that is, the child class overrides the parent class method. It requires that both methods have the same number and type of arguments and return value types , which are called method overrides.

(A subclass requires permission for a method override on a parent class, and the subclass method must be greater than or equal to the access rights of the parent class method.) That is, if the access rights of the parent method are default, then the subclass must be default or public, and if the parent method is public access, the subclass can only be public access. If the parent class method is private, private is not allowed to be accessed by any other class according to the wrapper principle of Java, so the subclass cannot overwrite the private method of the parent class . )

Iii. Summary

Compare items

Overload

Overwrite/rewrite

English name

Overload

Override

function signature

Requires only the number or type of arguments of a function to be inconsistent, no return value required

The function must have exactly the same number of arguments, type, and return values.

Happening Place

In the same class or two classes that have an inheritance relationship

Can only occur in two classes of an inheritance relationship

Access rights

No Limits

The subclass override method must have access permissions greater than or equal to the method of the parent class

Reference: http://www.cnblogs.com/kuillldan/p/5905572.html

The inheritance, overloading and rewriting of the basic knowledge--java class

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.