Home  >  Article  >  Java  >  How to define and use Java rewriting, overloading, and polymorphism

How to define and use Java rewriting, overloading, and polymorphism

王林
王林forward
2023-05-04 17:22:07849browse

1. Rewrite:

a.Definition:

Rewriting is the process of rewriting the implementation of methods of the parent class that are allowed to be accessed by the subclass. Neither the return value nor the formal parameters can be changed. That is, the shell remains unchanged and the core is rewritten!

The advantage of overriding is that subclasses can define their own behavior as needed. In other words, subclasses can implement the methods of the parent class as needed.

Overriding methods cannot throw new checked exceptions or exceptions that are broader than those declared by the overridden method. example:

class Animal{

public void move(){

System.out.println("Animals can move");

} }

class Dog extends Animal{

public void move(){

System.out.println("Dogs can run and walk");

} }

Then we quote:

Animal a = new Animal(); // Animal object

Animal b = new Dog(); // Dog object

a.move();//Execute the method of Animal class

b.move();//Execute the method of Dog class

The output is:

动物可以移动
狗可以跑和走

As you can see in the above example, although b belongs to the Animal type, it runs the move method of the Dog class (the method we override)

This is because during the compilation phase, only the reference type of the parameter is checked.

However, at runtime, the Java Virtual Machine (JVM) specifies the type of object and runs the object's methods.

Therefore, in the above example, the compilation is successful because the move method exists in the Animal class. However, at runtime, the method of the specific object is run.

b. Rules:

1. The parameter list must be exactly the same as that of the overridden method.

2. The return type can be different from the return type of the overridden method, but it must be a derived class of the parent class's return value.

3. The access permission cannot be lower than the access permission of the overridden method in the parent class.

4. The member methods of the parent class can only be overridden by its subclasses. If a subclass in the same package can override all parent class methods, except methods declared as private and final.

5. Methods declared as final cannot be overridden, while methods declared as static cannot be overridden, but can be declared again.

6. If the subclass and the parent class are not in the same package, the subclass can only override the non-final methods of the parent class that are declared public and protected.

7. The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, an overridden method cannot throw new mandatory exceptions, or mandatory exceptions that are broader than those declared by the overridden method, and vice versa.

2. Overload:

It has similar meaning to c.

Overloading is a method with the same name but different parameters in a class. The return types can be the same or different.

Each overloaded method (or constructor) must have a unique list of parameter types.

The most commonly used place is the overloading of the constructor.

The difference from overriding is that overloading focuses on different parameters.

3. Polymorphism:

a.Definition:

Polymorphism is the ability of the same behavior to have multiple different manifestations or shapes. Polymorphism is the same interface, using different instances to perform different operations.

Learning the concept of polymorphism is very important for applying interfaces well.

Three necessary conditions for the existence of polymorphism:

1.Inherit

2. Rewrite

3. The parent class reference points to the subclass object

For example, in this example, a reference of a parent class points to an object of a subclass:

Parent p = new Child();

Note: When calling a method using polymorphism, first check whether the method exists in the parent class. If not, a compilation error will occur; if so, call the method of the same name in the subclass.

b. Virtual function:

Virtual functions are one of the foundations of polymorphism. Of course, all functions in JAVA are virtual functions in the C sense by default, so you don’t need to pay special attention to it, because dynamic binding is the default behavior of Java.

If you do not want a function to have virtual function characteristics in Java, you can add the final keyword to make it a non-virtual function.

The above is the detailed content of How to define and use Java rewriting, overloading, and polymorphism. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete