[JavaSE column 59] The concept and priority of method rewriting, object-oriented polymorphism mechanism

Author homepage : Designer Xiao Zheng
Author brief introduction : 3 years of JAVA full-stack development experience, focusing on JAVA technology, system customization, remote guidance, dedicated to enterprise digital transformation, certified lecturer of CSDN College and Blue Bridge Cloud Course.
Main direction : Vue, SpringBoot, WeChat applet

This article explains the concept and priority of method rewriting in Java, and gives sample code.

1. What is method rewriting

Method overriding is a mechanism for achieving polymorphism in Java . When a subclass inherits from a parent class, the subclass can override the existing methods in the parent class to implement its own specific logic or behavior. Method overriding requires the method in the subclass to have the same method name, parameter list, and return type as the overridden method in the superclass.

The characteristics of method rewriting are as follows 4 4At 4 o'clock, please study hard.

  1. An overridden method in a subclass must have the same method signature as the overridden method in the superclass.
  2. The access modifiers of an overriding method cannot be more restrictive than the access modifiers of the overridden method. For example, if the method of the parent class is public, then the method overridden by the subclass must also be public.
  3. An overriding method cannot declare wider exceptions than the overridden method, but can declare narrower exceptions, or throw no exceptions.
  4. An overriding method cannot use a narrower access modifier than the overridden method, for example, if the superclass's method is protected, then the subclass's overridden method can be protectedor public, but not private.

The purpose of method rewriting is that in the inheritance relationship, subclasses can customize the methods of the parent class according to their own needs to achieve more flexible and specific functions. The rewriting method will dynamically determine which method to call at runtime. Implemented polymorphic features.

insert image description here


Second, why do you need to use method rewriting

The main reason for using method overriding in Java is to achieve polymorphism and code flexibility .

  1. Polymorphism : Method rewriting is one of the important means to achieve polymorphism. When the subclass inherits the parent class and rewrites the method of the parent class, it can point to the subclass object through the parent class reference, and call the corresponding rewritten method according to the actual object type. This allows the program to behave differently depending on the object at runtime.
  2. Flexibility : Method rewriting allows subclasses to customize the parent class' methods according to their own needs. Subclasses can rewrite parent class methods to achieve specific functions according to specific business needs. This can improve the flexibility and scalability of the code, making the program easier to maintain and expand.

In addition, method rewriting can also improve the readability and understandability of the code. By rewriting the method of the parent class in the subclass, the code can be more semantic and logical, making the program easier to understand.

In short, method rewriting is an important means to achieve polymorphism, improve code flexibility and readability, making Java programs more flexible, scalable and easy to understand.

insert image description here


3. Method rewriting priority problem

In Java, the priority of method overriding is based on the inheritance relationship .

If the subclass overrides the method of the parent class, the overridden method in the subclass will be called first when calling the method.

The following is a simple sample code to illustrate the priority of method rewriting, please study carefully.

class Animal {
    
    
    public void makeSound() {
    
    
        System.out.println("Animal makes sound");
    }
}

class Dog extends Animal {
    
    
    @Override
    public void makeSound() {
    
    
        System.out.println("Dog barks");
    }
}

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Animal animal = new Animal();
        animal.makeSound();  // Output: "Animal makes sound"

        Dog dog = new Dog();
        dog.makeSound();  // Output: "Dog barks"

        Animal animal2 = new Dog();
        animal2.makeSound();  // Output: "Dog barks"
    }
}

In the example above, the Animal class has a makeSound()method, and the Dog class inherits from the Animal class and overrides makeSound()the method. In main()the method, we create the objects of Animal class and Dog class respectively, and call their makeSound()method.

When animalthe object calls makeSound()the method, it calls the method in the Animal class makeSound(), output Animal makes sound.

When dogthe object calls makeSound()the method, it calls the overridden makeSound()method in the Dog class, output Dog barks.

When animal2the object (a reference variable of Animal type, pointing to an object of Dog type) calls makeSound()the method, due to the characteristics of polymorphism, it will also call the overridden makeSound()method in the Dog class, output Dog barks.

This shows that in method rewriting, the method call is based on the actual type of the object, not the type of the variable, so the method rewritten by the subclass will be called first.

insert image description here


4. Method of Rewriting Interview Questions

  1. What is method overriding? Please give an example.
  2. What is the difference between method overriding and method overloading?
  3. What are the rules for method overriding?
  4. If the method of the parent class is private, can it be overridden by the subclass?
  5. If a subclass overrides a parent class method, can it throw exceptions that are wider than the parent class method declaration?
  6. Can a static method of a parent class be overridden by a subclass?
  7. If the method of the parent class is final, can it be overridden by the subclass?
  8. When calling an overridden method, do you call the method of the parent class or the method of the subclass?
  9. Can the overridden method of the subclass be called in the parent class?
  10. What is the relationship between method overriding and polymorphism?

V. Summary

This article explains the concept and priority of method rewriting in Java, and gives sample code. In the next blog, I will explain the knowledge points of static code blocks in Java.

insert image description here

Guess you like

Origin blog.csdn.net/qq_41464123/article/details/131990867