Home > Java > javaTutorial > How to use java dynamic binding method

How to use java dynamic binding method

WBOY
Release: 2023-04-20 09:49:06
forward
909 people have browsed it

Usage

1. When the program is compiled, it actually calls the eat method of the parent class, but at runtime, it runs the eat method of the subclass. Binding occurred during runtime.

2. Use the preamble, first transform upward, and call the overridden method of the parent class and subclass with the same name through the parent class reference

Instance

package chapeter04;
 
class Test
{
public Test() { }
public void setName(String n)
{
this.name=n;
System.out.println("在父类中");
}
public String getName()
{
return this.name;
}
private String name;
}
 
public class Sample4_12 extends Test
{
public void setArea(String a)
{
this.area=a;
}
 
public String getArea()
{
return this.area;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Sample4_12 child = new Sample4_12();
Test test []=new Test[2];
test[0]=child;
test[0].setName("silence");
test[1]=new Test();
}
private String area;
 
}
Copy after login

The above is the detailed content of How to use java dynamic binding method. 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