Chapter 5 inheritance of notes of Java core technology Volume 1

5.1.3 dynamic binding

(1) The compiler views the method name.

(2) The compiler looks at the method type.

(3) Find a more appropriate way. Contains methods and parameters. Methods closer to the current position, such as f(int) and f(double). If the parameters are int, int will be automatically selected; Both the subclass and the parent class have methods with the same name. If the subclass has methods, the subclass method is preferred.

(4) Compilation errors: multiple matches and zero matches

Example code:

public class Main {

    public static void main(String[] args){

        Main solution = new Main();

        //Multiple matches found

        solution.f((short)5);

        //Zero matches found

        solution.k();

    }



    public void f(Integer a){

        System.out.println("int");

    }



    public void f(Double a){

        System.out.println("double");

    }

}

OTHER: JDK1.5 previously, the return type subclass was required to be consistent with the parent class. Now, the return type subclass needs to be a subclass of the return type in the parent class

ASon is a subclass of type A, so running the following code will not produce an error:

Example code:

public class Main {

    public static void main(String[] args){

        Main solution = new Main();

        solution.new B().getA();

        solution.new BSon().getA();

    }



    class A{

        public A(){

            System.out.println("create A");

        }

    }



    class ASon extends A{

        public ASon(){

            System.out.println("create ASon");

        }

    }



    class B{

        public A getA(){

            return new A();

        }

    }



    class BSon extends B{

        public ASon getA(){

            return new ASon();

        }

    }

}

(3) private static final method or constructor. The compiler can exactly which method to call, which is called dynamic binding.

The calling method depends on the actual type of implicit parameters, which is called static binding. Compared with dynamic binding, static binding requires no lookup process and is faster.

Private: private. Methods cannot be inherited, so they can be statically bound;

Static: static, you can use subclasses to hide, but you do not need to use subclass methods for upward transformation, and you must use parent methods for upward transformation, so you can bind statically;

final: finally, it cannot be rewritten, implies private, and can be statically bound.

reference material: https://cloud.tencent.com/developer/article/1121665

The subclass implicitly calls the parent class resolution process:

(1) Extract subclass parent method table

(2) Search the method in the current class. If it cannot be found, search the parent class method

(3) Find the method, call and return

Advantages of dynamic binding: there is no need to modify the existing code, and the reuse and expansion of the program can be completed through the inheritance relationship.

5.1.4 final keyword

Purpose:

  1. Variables decorated with final are constants. You can only select one of assignment, initialization block and constructor for assignment. Values of value types and reference type references cannot be modified later.
  2. Methods that use the final modifier are not rewritable.
  3. A class decorated with final, which cannot be inherited. This means that none of its methods can be overridden.

Q: is it necessary to use final for all methods and classes that do not need to inherit?

A: No. In early Java, it was necessary to optimize short methods; After the JIT compiler appears, it can automatically inline short and frequent methods, so there is no need to deliberately define final for all subclasses.

5.1.5 cast type

Basic types can be cast using parentheses. Similarly, reference types can also be cast, but it is only applicable to variables that are originally this type, otherwise an error will be reported.

Example code:

public class Main {

    public static void main(String[] args){

        Main solution = new Main();

        A a = solution.new A();

        ASon aSon = solution.new ASon();

        A tmp = aSon;//Automatic conversion

        ASon tmp2 = (ASon) tmp;//Force conversion

        ASon tmp3 = (ASon) a;//report errors

    }



    class A{

        public A(){

            System.out.println("create A");

        }

    }



    class ASon extends A{

        public ASon(){

            System.out.println("create ASon");

        }

    }



    class B{

        public A getA(){

            return new A();

        }

    }



    class BSon extends B{

    }

Operation results:

Series content:

Notes on Java core technology Volume 1: Chapter 1 overview of Java programming

Notes on Java core technology Volume 1: Chapter 2 Java programming environment

Notes on Java core technology Volume 1: Chapter 3 basic programming structure of Java (1)

Notes on Java core technology Volume 1: Chapter 3 basic programming structure of Java (2)

Notes on Java core technology Volume 1: Chapter 3 basic programming structure of Java (3)

Notes on Java core technology Volume 1: Chapter 3 basic programming structure of Java (4)

Notes on Java core technology Volume 1: Chapter 3 basic programming structure of Java (5)

Notes on Java core technology Volume 1: Chapter 3 basic programming structure of Java (6)

Notes on Java core technology Volume 1: Chapter 3 basic programming structure of Java (7) large number processing, array, multi-dimensional array and console parameter transmission

Notes to Java core technology Volume 1 Chapter 4: classes and objects

Chapter 4: classes and objects (2) Gregorian calendar and the basic composition of classes

Chapter 4: classes and objects (3) constructor global private methods

Chapter 4: classes and objects (4) static fields + static methods + factory methods

Chapter 4 of notes of Java core technology Volume 1: class and object (5) default value and default construction of formal parameter and argument constructor

Chapter 4 of notes of Java core technology Volume 1: class and object (6) constructor call and initialization block

Chapter 4 notes of Java core technology Volume 1: class and object (7) annotation, JavaDoc and class design

Chapter 5 inheritance of notes of Java core technology Volume 1

If you like, point a praise ~! Usually do the title, and notes will be updated to the official account.

Pay attention to official account and learn from each other: knowledge of Yu Niang Niang

Keywords: Java Back-end

Added by quicknb on Mon, 24 Jan 2022 23:20:45 +0200