Java (9) inheritance

Source: Internet
Author: User

The inherited class is called the superclass superclass, and the class inheriting the superclass is called the subclass subclass

The parent class is a one-to-many relationship, and the subclass is a one-to

Class name extends parent class names

The access restrictions and state of the current member variable that the modifier modifies. Public, Protected,default, private, static, final

Default same package All classes are accessible

Protected all classes and different packages of the same package inherit its subclasses (after inheriting subclasses have members of the parent class)

Overwrite of member variable: Static will not be overwritten

overriding override: A subclass method that, if it has the same name as the inherited method, constitutes an override of the method, overwriting. Member variables cannot be overridden when modified by static. Compile-time bike type, run-time racing_cycle type. Static can only have one copy, which is called through the class.

Overloaded Overload: In a class, there are two or two methods with the same name and different parameters, and the return value can be the same or different.

return type of override: The return type must be the same as the base type when the reference type can be its derived type

Overrides are inherited based, static modified methods cannot be overridden, and the only

A static method of a parent class can be overridden by a static method of a class

A non-static method of a parent class cannot be overridden by a static method of a class

A static method of a parent class cannot be overridden by a non-static method of a class

Overrides are used between the parent class and the child class

Overrides are used in the same class, with the same method name, but with different parameters.

Covered:

The parameter list of the overridden method must be exactly the same as the parameter list of the overridden method

The return value of the overridden method must be the same as the return value of the overridden method

The exception that is thrown by the overridden method must be the same as the exception that is thrown by the overridden method, or its subclass

The overridden method cannot be private, otherwise only a method is defined in its subclass and is not overwritten

Overload:

Only a different parameter list can be defined when using overloading

Overloads of methods cannot be made by overloading the return type, access rights, and exceptions thrown by the method

Rewrite:

The overridden method exists in the parent class and is also present in the child class.

The overridden method takes the form of polymorphism at run time

Overridden methods (subclasses) cannot have a higher access limit than the overridden method (the parent class). (The parent class should be replaced with a subclass in any case.) But if your parent's method is friendly, and you change it to private when the subclass is rewritten, what should the system do when someone else uses the parent class to refer to your child class method? (Private is not accessible) doing so will bring contradictions to the programming principles of polymorphism. Reflection can dynamically load a class and create its objects. In other words, the actual type of the object in this code can be known only if the program is running to a code. If the members of a class are not predictable in the level of access to the subclass, the attack is pretty deadly!! To be serious, you dare not use dynamic binding, and polymorphism becomes empty talk. )

The overridden method cannot have more exceptions than the overridden method

Java (9) inheritance

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.