Here, I mainly want to record the basic knowledge inherited from the point of view. It is a small summary so that I can check it in the future and facilitate beginners to learn. This article focuses on the Knowledge summary and will not elaborate on the principle in detail, of course, I think it is necessary to talk about it. Let's talk about it from several aspects.
1. What do subclass inherit from parent classes?
- Subclass and parent class are in the same package: subclass inherits the public and protected attributes of the parent class, and default access-level member variables and member methods;
- Subclass and parent class are no longer in the same package: subclass inherits the public and protected access-level member variables and member methods of the parent class;
2. What is method overloading?
If the method names of the two methods are the same, but the parameters are inconsistent, one method can be considered as another method to be overloaded.
3. What conditions should the overload method meet?
- The method name is the same; (required)
- The return types of methods can be different;
- Methods can have different modifiers;
- Method parameter type, order, and number must be at least one different (required)
4. What is method override?
A subclass defines a method. Its name, return type, and parameter signature exactly match a method in the parent class, return type, and parameter signature, so they are overwritten.
5. What should I pay attention to when overwriting methods?
- When the Java compiler encounters a subclass of a method with the same name as the parent class, it will first determine their Parameter Signatures. If the two are consistent, the compiler considers that child classes want to overwrite the methods of the parent class, and then analyzes their return types. If the types are the same, it indicates that the methods are overwritten; otherwise, the method is overloaded;
- The method of the subclass cannot reduce the access permission of the parent class method. That is to say, if the access permission of a method of the parent class is protected, if the subclass wants to override the method of the parent class, the access permission must be at least protected, but public, but not private;
- The subclass method cannot throw more exceptions than the parent class. The exception thrown by the subclass method must be the same as that thrown by the parent class method, or the subclass method throws an exception, which is a subclass of the exception class thrown by the parent class method;
- Method override exists between the subclass and parent class, that is, it does not exist in the same class;
- Static Methods of the parent class cannot overwrite non-static methods of the quilt class;
- Subclass can define a static method with the same name as the static method of the parent class, so that the static method of the parent class can be hidden in the subclass;
- Non-static methods of the parent class cannot be overwritten by the quilt class as static methods;
- The private method of the parent class cannot be overwritten by the quilt class;
- The abstract method of the parent class can be used by the quilt class in two ways: first, the Child class implements the abstract method of the parent class, but the child class re-declares the abstract method of the parent class;
- Non-abstract methods of the parent class can be overwritten as abstract methods;
6. What is the difference between the static method of hiding the parent class from the subclass and the instance method of overwriting the parent class from the subclass?
During runtime, the Java Virtual Machine binds the static method to its class, And the instance method to its object. The following example shows the Java code.
- Public class Base {
- Void method (){
- System. out. println ("Method of Base ");
- }
- Public static void staticMethod (){
- System. out. println ("Static method of Base ");
- }
- }
- Public class Sub extends Base {
- Public void method (){
- System. out. println ("Method of Sub ");
- }
- Public static void staticMethod (){
- System. out. println ("Static method of Sub ");
- }
- }
- Public class Test {
- Public static void main (String args []) {
- Base sub1 = new Sub ();
- Sub1.method (); // print method of sub
- Sub1.staticMethod (); // print Static method of Base;
- Sub sub2 = new Sub ();
- Sub2.method (); // print method of sub
- Sub2.staticMethod (); // print Static method of Sub;
- }
- }
Public class Base {void method () {System. out. println ("Method of Base");} public static void staticMethod () {System. out. println ("Static method of Base") ;}} public class Sub extends Base {public void method () {System. out. println ("Method of Sub");} public static void staticMethod () {System. out. println ("Static method of Sub") ;}} public class Test {public static void main (String args []) {Base sub1 = new Sub (); sub1.method (); // print method of sub sub1.staticMethod (); // print Static method of Base; Sub sub2 = new Sub (); sub2.method (); // print method of sub sub2.staticMethod (); // print Static method of Sub ;}}
7. The super keyword can only be used in the constructor or instance method, but not in the static method and static code block.
8. problems frequently encountered in polymorphism?
- For a variable of the reference type, the Java compiler processes the variable according to the declared type (during compilation), for example, Base s = new Sub (); the Base and Sub classes both have the var attribute, so when we access s. var refers to the var variable value in the Base class;
- Forced type conversion is required for downward transformation. For example, Base s = new Sub (); (Sub) s;
- For a variable of the reference type, the Java Virtual Machine processes the variable according to the object actually referenced by it at runtime;
- In the runtime environment, when you access the methods and attributes of the referenced object by referencing type variables, the Java virtual machine uses the following binding rules:. the instance method is bound to the method of the object actually referenced by the reference variable. This binding is dynamic, because it is dynamically determined by the Java Virtual Machine at runtime;
B. The static method is bound to the method of the type declared by the reference variable. This binding is a static binding because it has been bound during the compilation phase;
C. bind member variables (including static variables and instance variables) to the member variables of the type declared by the referenced variables. Such binding is static binding because it is actually the binding made during the compilation phase.