This article brings you relevant knowledge about java. After the subclass inherits the parent class, you can write a method in the subclass with the same name and the same parameters as the parent class, so as to realize the function of the parent class. We call the overwriting of methods with the same name and parameters in a class called method rewriting. Let’s take a look at it together. I hope it will be helpful to everyone.

Recommended study: "java video tutorial"
1. Meaning
After a subclass inherits a parent class , you can write a method in the subclass with the same name and the same parameters as the parent class, thereby overriding the method with the same name and the same parameters in the parent class. We call this process override of the method
2. Why use method overriding
2.1 When the method of the parent class cannot meet the needs of the subclass, the method needs to be rewritten in the subclass
2.2 Question and analysis
For example, there is a parent class Peple and a subclass Chinese. There is a say() method in the parent class that outputs people talking. However, when I want the subclass to call it, it outputs Chinese people talking. It is very difficult. Obviously calling the method directly is not possible, so you need to rewrite the say method in the subclass
2.3 Sample code
People class
public class Peple {
private String name;
private String sex;
private int age;
private int weight;
public Peple() {
}
public Peple(String name, String sex, int age, int weight) {
this.name = name;
this.sex = sex;
this.age = age;
this.weight=weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void say(){
System.out.println("人在说话");
}
}Chinese class
public class Chinese extends Peple{
public Chinese() {
}
@Override
public void say() {
System.out.println("中国人在说话");
}
} Test03 Class
public class Test03 {
public static void main(String[] args) {
Chinese c=new Chinese();
c.say();
//当进行方法重写时,调用的是子类的say()方法
}
}2.4 Sample code running screenshot

3. How to use method overriding
3.1 Basic Syntax
@Override
权限修饰符 返回值类型 方法名(形参列表){
//子类重写的方法的权限修饰符的访问权限必须大于等于父类的,但是父类不能是private
//当父类的返回值类型为基本数据类型或者为void时,子类方法的返回值类型也应该为对应的基本数据类型或者void
}3.2 Specific analysis
3.2.1 The access permissions of methods overridden by subclasses should be greater than or equal to the access permissions of parent class methods
a Sample code
People class
public class Peple {
private String name;
private String sex;
private int age;
private int weight;
public Peple() {
}
public Peple(String name, String sex, int age, int weight) {
this.name = name;
this.sex = sex;
this.age = age;
this.weight=weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
//没有写访问权限的话,默认是default访问权限
void say(){
System.out.println("人在说话");
}
}Chinese class
public class Chinese extends Peple{
public Chinese(){
}
//父类say方法的访问权限为default,子类say方法的访问权限为public,
// 符合子类方法访问权限大于等于父类方法访问权限的要求
@Override
public void say() {
System.out.println("中国人在说话");
}
}Test03 class
public class Test03 {
public static void main(String[] args) {
Chinese c=new Chinese();
c.say();
}
}b Sample code running screenshot

3.2.2 When the return value type of the parent class method is a basic data type, the return value type of the method overridden by the subclass is also the corresponding basic data type
a Sample code
People class
public class Peple {
private String name;
private String sex;
private int age;
private int weight;
public Peple() {
}
public Peple(String name, String sex, int age, int weight) {
this.name = name;
this.sex = sex;
this.age = age;
this.weight=weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public double add(int a,int b){
return a+b;
}
}Chinese class
public class Chinese extends Peple{
public Chinese(){
}
@Override
public double add(int a,int b) {
return a+b+1;
}
}Test03 class
public class Test03 {
public static void main(String[] args) {
Chinese c=new Chinese();
System.out.println("求和之和再加上1的结果为: "+c.add(2, 3));
}
}b Sample code running screenshot

3.2.3 When the return value type of the parent class method is void, the return value type of the method overridden by the subclass is also void
a Sample code
People class
public class Peple {
private String name;
private String sex;
private int age;
private int weight;
public Peple() {
}
public Peple(String name, String sex, int age, int weight) {
this.name = name;
this.sex = sex;
this.age = age;
this.weight=weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public void eat(){
System.out.println("人的主食一般是熟食");
}
}Chinese class
public class Chinese extends Peple{
public Chinese(){
}
@Override
public void eat() {
System.out.println("中国人的主食是大米或者小麦");
}
}Test03 class
public class Test03 {
public static void main(String[] args) {
Chinese c=new Chinese();
c.eat();
}
}b Sample code running screenshot

3.2.4 When the parent When a class method is statically modified, subclasses cannot override the method
a Error example code
People class
public class Peple {
private String name;
private String sex;
private int age;
private int weight;
public Peple() {
}
public Peple(String name, String sex, int age, int weight) {
this.name = name;
this.sex = sex;
this.age = age;
this.weight=weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public static void eat(){
System.out.println("人的主食一般是熟食");
}
}Chinese class
public class Chinese extends Peple{
public Chinese(){
}
@Override
public void eat() {
System.out.println("中国人的主食是大米或者小麦");
}
}Test03 Class
public class Test03 {
public static void main(String[] args) {
Chinese c=new Chinese();
c.eat();
}
}b Sample code running screenshot
Error message given by idea when compiling

Given after forced running Error message

3.3 Some tips on method rewriting
3.3.1 Copying method
steps
1. First directly copy (Ctrl C) the method in the parent class that needs to be overridden by the subclass
2. Paste (Ctrl V) into the subclass
3 .Modify the functions in the subclass to facilitate the realization of requirements that cannot be achieved by the parent class
Operation screenshot display




##3.3.3 Shortcut key method
Steps
1. Move the mouse to the location where the overridden method should be generated
2. Press the Alt key and Insert key on the keyboard at the same time,
3. In the pop-up box, select Override Methods
4. After double-clicking, an interface will pop up. In the interface, select the method that needs to be overridden by the subclass
5. Click the OK button The required rewrite method will be generated later
6. Remove the automatically generated first line in the rewrite method, and then write the appropriate statement in its method body
Operation screenshot display




java video tutorial 》
The above is the detailed content of Complete mastery of method overriding in Java. For more information, please follow other related articles on the PHP Chinese website!
What software do graphic designers use for logosJul 21, 2025 am 01:34 AMAdobeIllustratoristhemostcommonlyusedsoftwareforlogodesignduetoitsvector-basedcapabilities,allowingscalabledesignswithoutlossofquality,alongwithprecisecontrolovershapes,typography,andcolor.CorelDRAWservesasabeginner-friendlyalternative,especiallyforW
How to fix 'The request could not be performed because of an I/O device error' on external driveJul 21, 2025 am 01:34 AMWhen encountering "I/O device error", first check for connection and hardware problems, such as replacing the data cable, USB interface, or trying another computer; secondly restarting the Explorer or computer to solve system stuttering; then use the disk checking tool to repair file system errors; then update or reinstall the driver of the external hard drive; finally be sure to safely eject the device to avoid forced unpluging. Follow the above steps to troubleshoot in turn, which can usually resolve I/O errors caused by unstable connection, driver exceptions, or file system corruption.
What is a software keylogger and how to detect itJul 21, 2025 am 01:10 AMSoftware keyboard loggers capture keyboard input through the background running and are often used to steal sensitive information. It may be installed through malicious downloads, phishing emails, disguised updates, etc., and hide processes or modify attributes to evade detection. To detect the keyboard logger, you can 1. Check whether there are unfamiliar programs in the startup item; 2. Observe abnormal behaviors such as cursor movement and typing delay; 3. Use task manager or ProcessExplorer to find suspicious processes; 4. Use Wireshark or firewall tools to monitor abnormal network traffic; 5. Run anti-malware such as Malwarebytes and HitmanPro to scan and clear it. In terms of prevention, we should do: 1. Do not download software from unknown sources; 2. Do not click on suspicious links or attachments from emails; 3. Guarantee
Wifi keeps disconnectingJul 21, 2025 am 01:09 AMFrequent Wi-Fi disconnection is mainly caused by router location, device problems, improper settings or network service provider failure. 2. First check whether the router is placed in the center, reduce obstacles and switch to the 5GHz band or replace the crowded channel. 3. Secondly, restart the device, update the system and driver, and reconnect Wi-Fi to troubleshoot the device itself. 4. Then log in to the router background to view the logs, check DHCP settings, turn off the energy saving mode and ensure that the Mesh network signal is good. 5. Finally, if multiple devices are disconnected at the same time and the speed measurement is abnormal, it may be a problem with the service provider. You need to contact customer service to deal with line or account failure.
How to fix 'The device is not ready' errorJul 21, 2025 am 01:03 AMWhen encountering the "Thedeviceisnotready" error, first confirm whether the device's physical connection is stable and check the device status; 1. Re-plug and unplug the device, try to replace the USB interface, and avoid using the hub; 2. Observe whether the device has abnormal noise or indicator problems; 3. Restart Windows Explorer or directly restart the computer; 4. Open the Device Manager to update or reinstall the corresponding driver; 5. Run the command prompt as an administrator, execute the chkdsk command to check and repair disk errors; If the above steps are invalid, it may be that the device hardware is damaged and needs to be replaced or repaired.
Why is a software asking for administrator rightsJul 21, 2025 am 01:02 AMSoftware requests administrator permissions for three reasons: First, system-level modifications are required, such as writing to the system directory or changing the registry when installing the software; second, accessing restricted resources, such as scanning deep system files or operating protected hardware; third, improper design, some programs unnecessarily require administrator permissions. Therefore, when encountering such a prompt, you should first confirm the credibility of the program before deciding whether to authorize it.
Why is my washing machine shaking violently?Jul 21, 2025 am 01:01 AMRapid shaking of the washing machine is usually caused by imbalance, including uneven loading of clothing, unmounted machine, wear of suspension components or unremoved transport bolts. 1. Uneven distribution of clothes is the most common reason. Thick clothes should be placed evenly and overloaded; 2. The machine is not level and will cause shaking. The floor flatness and casters should be checked. Stable plates should be installed on the carpet; 3. Wearing of the suspension components may cause vibration and abnormal noise from the air machine, and the maintenance costs are high; 4. The transport bolts are not removed, which will also cause violent shaking. The bolts at the rear should be checked and removed. It is recommended to prioritize simple problems such as load balancing, horizontal status and transport bolts, and then consider mechanical failures.
How to fix error 0x800f0923Jul 21, 2025 am 12:58 AMError 0x800f0923 is usually caused by system file corruption, network configuration abnormalities or Windows update cache problems. Solutions include: 1. Reset the network components, run netshwinsockreset and netshintipreset through the administrator command prompt and restart; 2. Use SFC and DISM tools to repair system files and images; 3. Clear the Windows Update cache, and delete the SoftwareDistribution folder contents after stopping the service; 4. Manually download and install the .NETFramework update package from Microsoft's official website. It is recommended to try these methods in order, with SFC/DISM and network reset being particularly critical.

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software








