Comparison and difference between overloading and rewriting in Java

The difference between overloading and rewriting in Java

First, let's talk about overloading

(1) Method overloading is a means for a class to handle different types of data in a unified way. Multiple functions with the same name exist at the same time, with different number / types of parameters.

Overloading is a manifestation of polymorphism in a class.

(2) Java method overloading means that multiple methods can be created in a class. They have the same name, but have different parameters and different definitions.

When a method is called, it determines which method to use by the number and type of different parameters passed to them, which is called polymorphism.

(3) When overloading, the method name should be the same, but the parameter types and numbers are different, and the return value types can be the same or different. The return type cannot be used as the distinguishing standard of overloaded functions.

The following is an example of overloading:

Different overloaded method barks are distinguished according to their parameter types.

Then let's talk about overriding

(1) The polymorphism between parent and child classes redefines the functions of the parent class. If a method defined in a child class has the same name and parameters as its parent class, we say that the method is overridden. In Java, subclasses can inherit the methods in the parent class without rewriting the same methods.

However, sometimes subclasses do not want to inherit the methods of the parent class intact, but want to make certain modifications, which requires method rewriting.

Method overrides are also called method overrides.

(2) If a method in the subclass has the same method name, return type and parameter table as a method in the parent class, the new method will overwrite the original method.

If you want the original method in the parent class, you can use the super keyword, which refers to the parent class of the current class.

(3) The access rights of subclass functions cannot be less than those of the parent class;

Here is an example of Rewriting:

Concept: that is, the mechanism for calling object methods.

Inside story of dynamic binding:

1. The compiler checks the type and method name declared by the object to get all candidate methods. Try to comment out the test of the base class in the above example. At this time, the compilation cannot pass.

2. Overload decision: the compiler checks the parameter type of the method call and selects the only one from the above candidate methods (there will be implicit type conversion).

If the compiler finds more than one or fails to find one, the compiler will report an error. Try to comment out the test (byte b) of the base class in the above example. At this time, the running result is 1.

3. If the method type is priavte static final and Java adopts static compilation, the compiler will know exactly which method to call.

4. When the program runs and uses dynamic binding to call a method, the virtual machine must call the method version matching the actual type of the object.

In the example, the actual type pointed to by B is testoverriding, so B. test (0) calls test of the subclass.

However, the subclass does not override test (byte b), so b.test ((byte) 0) calls the test (byte b) of the parent class.

If the (byte b) of the parent class is commented out, the implicit type is converted to int in the second step, and finally the test (int i) of the child class is called.

***

Polymorphism is a feature of object-oriented programming, which has nothing to do with methods,

In short, the same method can make different processing according to different input data, that is, the of the method

Overloading -- there are different parameter lists (static polymorphism)

When a subclass inherits from the same method of the parent class and has the same input data, but needs to make a response different from that of the parent class, you have to override the parent class method,

That is, override the method in subclasses -- the same parameters, different implementations (dynamic polymorphism)

OOP has three characteristics: inheritance, polymorphism and encapsulation.

At this time, the output result is 1 0, which is the result of dynamic binding at run time.

The main advantage of rewriting is that it can define the characteristics unique to a subclass:

This is also called polymorphism. Rewriting methods can only exist in inheritance relationships, and rewriting methods can only override non private methods of the parent class.

When the father class speak() method in the above example is private, the son class cannot override the father class speak() method. At this time, the son class speak() method is equivalent to a speak() method defined in the son class.

Once the father class speak() method is final, the son class cannot override the father class speak() method at all, regardless of whether the method is modified by public, protected or default,

When you try to compile code, the compiler reports an error. Example:

When the father class speak () method is modified by default, it can only be overridden by its subclasses in the same package. If it is not in the same package, it cannot be overridden.

When the father class speak () method is protoeted, it can be overridden not only by its subclasses in the same package, but also by subclasses in different packages.

Rules for overriding methods:

1. The parameter list must be exactly the same as the overridden method, otherwise it cannot be called overridden but overloaded.

2. The return type must always be the same as the return type of the overridden method, otherwise it cannot be called overridden but overloaded.

3. The access modifier must be more restrictive than the access modifier of the overridden method (public > protected > Default > private)

4. Rewriting methods must not throw new check exceptions or check exceptions that are broader than the declaration of the rewritten method. For example:

A method of the parent class declares a check exception IOException. When overriding this method, you cannot throw an exception. You can only throw subclass exceptions of IOException, and you can throw non check exceptions.

Overloaded rules:

1. Must have different parameter lists;

2. You can have non scolding return types as long as the parameter lists are different;

3. There can be different access modifiers;

4. Different exceptions can be thrown;

Rewriting differs from overloading in that:

Rewriting polymorphism works. Calling overloaded methods can greatly reduce the amount of code input. As long as the same method name passes different parameters, it can have different functions or return values.

Using rewriting and overloading well can design a class with clear and concise structure. It can be said that rewriting and overloading play an extraordinary role in the process of writing code

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>