Detailed explanation of inheritance examples of Java classes (power node Java College)

I Do you know class?

In Java, class files are For code files with Java suffix, at most one public class is allowed in each class file. When there is a public class, the name of the class file must be the same as that of the public class. If there is no public, the name of the class file can be any name (of course, names starting with numbers are not allowed).

Within the class, if the displayed assignment initialization is not performed for the member variables when they are defined, Java will ensure that each member variable of the class is properly initialized:

1) variables of basic data types such as char, short, byte, int, long, float and double will be initialized to 0 by default (Boolean variables will be initialized to false by default);

2) variables of reference type will be initialized to null by default.

If a constructor is not explicitly defined, the compiler will automatically create a parameterless constructor, but remember that if a constructor is explicitly defined, the compiler will not automatically add a constructor. Note that all constructors are static by default.

Let's focus on the initialization sequence:

When a program executes, it needs to generate an object of a class. The Java execution engine will first check whether the class is loaded. If it is not loaded, it will first load the class and then generate the object. If it has been loaded, it will directly generate the object.

During class loading, the static member variable of the class will be initialized. In addition, if there is a static statement block in the class, the static statement block will be executed. Static member variables and static statement blocks are executed in the same order as in the code. Remember, in Java, classes are loaded on demand. This class will be loaded only when it is needed, and will only be loaded once. Look at the following example to understand:

Run this code and you will find that "bread is loaded" will be printed only once.

In the process of generating an object, the member variables of the object are initialized first, and then the constructor is executed. That is, the variables in the class will be initialized before any method (including constructor) calls, even if the variables walk between the method definitions.

The output result is:

bread meal

II Do you know about inheritance?

Inheritance is an indispensable part of all OOP languages. The extensions keyword is used in Java to represent the inheritance relationship. When a class is created, it is always inherited. If the class to be inherited is not explicitly specified, it is always implicitly inherited from the root class object. For example, the following code:

Class man inherits from the person class. In this way, The person class is called the parent class (base class), and the man class is called the child class (exported class). If there is an inheritance relationship between two classes, the subclass will automatically inherit the methods and variables of the parent class, and the methods and variables of the parent class can be called in the subclass. In Java, only single inheritance is allowed, that is, a class can only inherit from one parent class. However, a class can be inherited by multiple classes, that is, a class can have multiple subclasses 。

  1. The subclass inherits the member variables of the parent class

When a subclass inherits a class, it can use the member variables in the parent class, but it does not fully inherit all the member variables of the parent class. The specific principles are as follows:

1) be able to inherit the public and protected member variables of the parent class; Cannot inherit the private member variable of the parent class;

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

3) for the parent class member variable that can be inherited by the child class, if there is a member variable with the same name in the child class, it will be hidden, that is, the member variable of the child class will shield the member variable with the same name of the parent class. If you want to access a member variable with the same name in the parent class in a subclass, you need to use the super keyword for reference.

  2. The subclass inherits the methods of the parent class

Similarly, subclasses do not fully inherit all methods of the parent class.

1) be able to inherit the public and protected member methods of the parent class; Cannot inherit the private member method of the parent class;

2) for the package access permission member method of the parent class, if the child class and the parent class are under the same package, the child class can inherit; Otherwise, subclasses cannot inherit;

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

Note: hiding and overwriting are different. Hiding is for member variables and static methods, while overriding is for ordinary methods. (to be discussed later)

  3. constructor

A subclass is a constructor that cannot inherit from the parent class, but it should be noted that if the constructors of the parent class are all with parameters, the constructor of the parent class must be explicitly called through the super keyword in the constructor of the subclass with an appropriate parameter 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 constructor of the child class. If the super keyword is not used, the system will automatically call the parameterless constructor of the parent class. It is clear from the following example:

There is no problem with such code. If the parameterless constructor of the parent class is removed, the following code will inevitably make an error:

Change it to the following:

  4. super

Super has two main uses:

  1)super. Member variable / super Membership method;

  2)super(parameter1,parameter2....)

The first usage is mainly used to call the same name member variable or method of the parent class in the subclass. The second method is mainly used to explicitly call the constructor of the parent class in the constructor of the subclass. Note that if it is used in the subclass constructor, it must be the first statement of the subclass constructor.

III Common written interview questions

1. What is the output of the following code?

This topic mainly investigates the calling order and initialization order of constructor when class inheritance. One thing to remember: the constructor call and initialization process of the parent class must be in front of the child class. Since the parent class of the circle class is the shape class, the shape class is initialized first, and then the constructor of the shape class is executed. Then initialize the subclass circle, and finally execute the constructor of circle.

2. What is the output result of the following code?

This question mainly examines the difference between hiding and covering (of course, it is also related to polymorphism, which will be discussed in subsequent blog posts).

Coverage is only for non static methods (the final state method cannot be inherited, so there is coverage), while hiding is for member variables and static methods. The difference between the two is that coverage is controlled by RTTI (runtime type identification) constraint, but the hidden method is not subject to this constraint. That is, only the override method will be dynamically bound, and the hidden method will not be dynamically bound. In Java, all methods except static method and final method are dynamically bound. Therefore, the above output results will appear.

The above is a detailed explanation of the inheritance examples of Java classes introduced by Xiaobian (organized by the power node Java College). I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>