Java: class and inheritance are undoubtedly the most important basis for object-oriented programming languages. Abstract, encapsulation, inheritance, and polymorphism are all inseparable from classes. Only existing classes can reflect the characteristics of object-oriented programming. Today we will learn about classes and inheritance. First, let's talk about things related to class initialization, and then explain how to inherit this feature from several aspects. The following is the directory outline of this article: 1. Do you understand the class? 2. Do you understand inheritance? Iii. common interview pen questions 1. Do you understand the class? In Java, class files are. java is a code file with a suffix. Only one public class can appear 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 no public name exists, the class file name can be any name (of course, names starting with numbers are not allowed ). In the class, if the value assignment Initialization is not performed during definition, Java will ensure that every member variable of the class is properly initialized: 1) for char, short, byte, int, long, float, double, and other basic data types, the default initialization value is 0 (the boolean variable is initialized to false by default); 2) for variables of the reference type, the default Initialization is null. If the constructor is not explicitly defined, the compiler will automatically create a non-argument constructor, but remember that if the constructor is explicitly defined, the compiler will not automatically add constructor. Next we will focus on the initialization sequence: when the 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, the class is loaded before the object is generated. If the class has been loaded, the object is generated directly. During class loading, static member variables 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. The execution sequence of static member variables and static statement blocks is the same as that in the code. Remember, in Java, classes are loaded on demand. Classes are loaded only once when they are used. Take the following example to understand: 1234567891011121314151617 public class Test {public static void main (String [] args) throws ClassNotFoundException {Bread bread1 = new Bread (); bread bread2 = new Bread () ;}} class Bread {static {System. out. println ("Bread is loaded");} public Bread () {System. out. println ("bread") ;}} when you run this code, you will find that "Bread is loaded" is printed only once. During object generation, the object's member variables are initialized before the constructor is executed. That is to say, the variables in the class will be initialized before any method (including the constructor) is called, even if the variables walk between method definitions. 12345678910111213141516171819202122 public class Test {public static void main (String [] args) {new Meal () ;}} class Meal {public Meal () {System. out. println ("meal");} Bread bread = new Bread ();} class Bread {public Bread () {System. out. println ("bread") ;}} the output result is: breadmeal 2. do you understand inheritance? Inheritance is an indispensable part of all OOP languages. In java, the extends keyword is used 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 inherited from the root class Object implicitly. For example, in the following code: 1234567891011 class Person {public Person () {}} class Man extends Person {public Man () {}} class Man inherits from the Person class, the Person class is called the parent class (base class), and the Man class is called the subclass (export class ). If the two classes have an inheritance relationship, the subclass automatically inherits the methods and variables of the parent class. In the subclass, you can call the methods and variables of the parent class. In java, only single inheritance is allowed. That is to say, a class can only be displayed and inherited from a parent class. However, a class can be inherited by multiple classes, that is, a class can have multiple subclasses. 1. Subclass inherits the member variables of the parent class. When the Child class inherits a class, the member variables in the parent class can be used, but not all member variables of the parent class are fully inherited. The specific principles are as follows: 1) It can inherit the public and protected member variables of the parent class; it cannot inherit the private member variables of the parent class; 2) It can access the member variables of the parent class package, if the subclass and the parent class are in the same package, the subclass can inherit; otherwise, the subclass cannot inherit; 3) for the parent class member variables that the subclass can inherit, if a member variable with the same name appears in the subclass, the member variable of the subclass hides the member variable with the same name as the parent class. If you want to access the member variables with the same name in the parent class in the subclass, you need to use the super keyword for reference. 2. The subclass inherits the methods of the parent class. Similarly, the subclass does not completely inherit all methods of the parent class. 1) It can inherit the public and protected member methods of the parent class; it cannot inherit the private member methods of the parent class; 2) for the parent class package access permission member methods, if the subclass and the parent class are in the same package, the subclass can inherit; otherwise, the subclass cannot inherit; 3) for the parent class member methods 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 variables of the subclass overwrite the member variables of the parent class with the same name. If you want to access the member methods of the same name in the parent class in the subclass, you need to use the super keyword for reference. Note: hiding and overwriting are different. Hiding targets member variables and static methods, while overwriting targets common methods. (As mentioned later) 3. the constructor subclass cannot inherit the constructor of the parent class, but note that if the constructor of the parent class has parameters, the constructor of the parent class must be called with the super keyword explicitly in the constructor of the subclass and configured with an appropriate parameter list. If the parent class has no parameter constructor, it is not necessary to use the super keyword in the constructor of the subclass to call the parent class constructor. If the super keyword is not used, the system automatically calls the non-argument constructor of the parent class. Take a look at the following example to understand: 123456789101112131415161718192021222324252627282930 class Shape {protected String name; public Shape () {name = "shape" ;}public Shape (String name) {this. name = name ;}} class Circle extends Shape {private double radius; public Circle () {radius = 0;} public Circle (double radius) {this. radius = radius;} public Circle (double radius, String name) {this. radius = radius; this. name = n There is no problem with the code like ame ;}}. if you remove the non-argument constructor of the parent class, the following code will inevitably fail: change to the following: 4. super has two main usage methods: 1) super. member variable/super. member method; 2) super (parameter1, parameter2 ....) the first method is mainly used to call the member variables or methods of the parent class in the subclass. The second method is mainly used to call the constructor of the parent class explicitly 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 Interview Questions 1. What are the output results of the following code? 12345678910111213141516171819202122232425262728 public class Test {public static void main (String [] args) {new Circle () ;}} class Draw {public Draw (String type) {System. out. println (type + "draw constructor") ;}} class Shape {private Draw draw = new Draw ("shape"); public Shape () {System. out. println ("shape constructor") ;}} class Circle extends Shape {private Draw draw = new Draw ("circle"); publ Ic Circle () {System. out. println ("circle constructor") ;}} shape draw constructorshape constructorcircle draw constructorcircle constructor this topic mainly deals with the call sequence and initialization sequence of the constructor during class inheritance. Remember that the constructor call and initialization process of the parent class must be prior to the subclass. Because the parent class of the Circle class is the Shape class, the Shape class first initializes and then executes the Shape class constructor. The next step is to initialize the subclass Circle, and finally execute the constructor of Circle. 2. What is the output result of the following code? 12345678910111213141516171819202122232425262728293031323334353637383940 public class Test {public static void main (String [] args) {Shape shape = new Circle (); System. out. println (shape. name); shape. printType (); shape. printName () ;}} class Shape {public String name = "shape"; public Shape () {System. out. println ("shape constructor");} public void printType () {System. out. println ("this is shape");} public static void printName () {System. out. println ("shape") ;}} class Circle extends Shape {public String name = "circle"; public Circle () {System. out. println ("circle constructor");} public void printType () {System. out. println ("this is circle");} public static void printName () {System. out. println ("circle");} shape constructorcircle constructorshapethis is circleshape