Home > Java > javaTutorial > body text

How to implement Java dynamic binding mechanism

PHPz
Release: 2023-04-18 17:10:03
forward
1100 people have browsed it

1. The process of dynamic binding:

Example:

public class Son extends Father  Son son = new Son();3 son.method();
Copy after login

1. First, the compiler searches for the corresponding class based on the declared type and method name of the object (Son) and its parent class (Father)'s "method table", find all method methods whose access attributes are public.

There may be multiple methods named method, but the parameter types or numbers are different.

2. Then, find the exact matching method based on the "signature" of the method.

The name and parameter list of a method are called the signature of the method.

In versions prior to Java SE 5.0, when overriding a parent class method, the return type must be the same. When a subclass overrides a superclass method, its return type is now allowed to be defined as a subtype of the original type.

public Father getFather(){...} //父类中的方法  public Son getFather(){...} //子类覆盖父类中的getFather()方法
Copy after login

3. If it is a private, static, final method or constructor, the compiler clearly knows which method to call, and this calling method is called "static calling".

4. Call the method.

If the method() method is defined in the subclass Son, the corresponding method in the subclass will be called directly; if the corresponding method is not defined in the subclass Son, then method() will be found in its parent class. method.

2. Demo

1. Subclass overrides the method in the parent class and calls the method in the subclass

public class Father{      public void method(){          System.out.println("父类方法:"+this.getClass());    }  }  public class Son extends Father{      public void method(){          System.out.println("子类方法"+this.getClass());  }  public static void main(String[] args){      Father instance = new Son();      instance.method();      }  }  //结果:子类方法:class Son
Copy after login

2. Subclass There is no overriding method in the parent class, so go to the parent class to find the corresponding method

public class Father{      public void method(){          System.out.println("父类方法:"+this.getClass());      }  }  public class Son extends Father{      public static void main(String[] args){          Father instance = new Son();          instance.method();       }  }  //结果:父类方法:class Son
Copy after login

3. Dynamic binding is only for object methods and is invalid for attributes. Because properties cannot be overridden.

public class Father{      public String name = "父亲属性";      }  public class Son extends Father{      public String name = "孩子属性";       public static void main(String[] args){          Father instance = new Son();          System.out.println(instance.name);      }  }  //结果:父亲属性
Copy after login

The above is the detailed content of How to implement Java dynamic binding mechanism. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template