Home > Java > javaTutorial > Overriding methods in Java

Overriding methods in Java

巴扎黑
Release: 2017-04-17 10:45:08
Original
2207 people have browsed it

In Java and some other advanced object-oriented programming languages, subclasses can inherit methods from the parent class without rewriting the same methods. But sometimes the subclass does not want to inherit the methods of the parent class unchanged, but wants to make certain modifications, which requires method rewriting. Method rewriting is also called method overwriting. The following will introduce you to java method rewriting and rewriting rules. Let’s learn together

1. Method override (Override)

How to define overriding in Java: The inheritance feature of a class in a Java program can generate a subclass. When the subclass inherits the parent class, it will have the non-private attributes (methods and variables) of the parent class. In Subclasses can add their own attributes (methods and variables), and they can also extend methods in the parent class to enhance their own functions. This is called rewriting, also known as copying or overriding. The so-called method rewriting means that the method of the subclass and the method inherited from the parent class have exactly the same method name, return value type, number of method parameters, and parameter type. Only in this way can it be called method rewriting.

Code reflection:


// 这是父类的定义
public class Person {
  public void eat() {
     System.out.println("=====这是父类Person的eat方法=======");
   }
}
// 这是子类的定义
public class Student extends Person {
  @Override
  public void eat() {
    System.out.println("===这是子类Student的eat方法===");
  }
  // main方法测试
  public static void main(String[] args) {
    Student student = new Student();
    student.eat(); //输出:===这是子类Student的eat方法===
   }
}
Copy after login

After the subclass overrides the method of the parent class, it is called when instantiating the subclass are the methods in the subclass, and the methods of the parent class are as if they were overridden. If you need to call a method of the parent class in a subclass, use the super keyword in the subclass method to call the method of the parent class. The format is: super. The method name (parameter list) in the parent class.

## Rewriting rules:

When rewriting methods, you need to follow the following rules to achieve method rewriting:

(1) The parameter list of the subclass method must be the same as the parameter list of the overridden method in the parent class (number of parameters and parameter type), otherwise only method overloading can be achieved.

(2) The return value type of the subclass method must be the same as the return value type of the overridden method in the parent class, otherwise only method overloading can be achieved.

(3) Java stipulates that the access rights of a subclass method cannot be less than the access rights of the overridden method in the parent class, and must be greater than or equal to the access rights of the parent class.

(4) During the rewriting process, if the overridden method in the parent class throws an exception, the method in the subclass will also throw an exception. However, there are certain constraints on the exceptions thrown ---> Subclasses cannot throw more exceptions than the parent class, they can only throw smaller exceptions than the parent class, or they cannot throw exceptions. For example: if the parent class method throws Exception, then the subclass can only throw IOException or an exception smaller than Exception or no exception.

The above is the detailed content of Overriding methods in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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