The difference between reload and rewriting in Java is that java reload Rewriting

Source: Internet
Author: User

The difference between reload and rewriting in Java is that java reload Rewriting

Notes for learning Java !!!
If you have any questions or want to obtain learning resources during the learning process, join the Java learning exchange group: 159610322 let's learn Java together!

First, let's talk about overload)


(1) method Overloading is a means for classes to process different types of data in a unified manner. Multiple Functions with the same name exist at the same time and have different parameter numbers/types.

Overload Overloading is a manifestation of polymorphism in a class.


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

When calling a method, the number and type of different parameters passed to them are used to determine which method to use. This is polymorphism.


(3) During overload, the method name must be the same, but the parameter type and number are different. The return value type can be the same or different. Return types cannot be used as the criteria for distinguishing heavy-duty functions.


The following is an example of heavy load:
Package c04.answer; // This is the package name
// This is the first programming method of this program. first create a Dog class instance in the main method, and then use the this keyword to call different bark methods in the construction method of the Dog class.

Different overload Methods bark are differentiated based on their parameter types.

// Note: Except the constructor, the compiler prohibits you from calling the constructor anywhere else.
Package c04.answer;

Public class Dog {
Dog ()
{
This. bark ();
}
The void bark () // bark () method is the overload method.
{
System. out. println (\ "no barking! \");
This. bark ("female \", 3.4 );
}
Void bark (String m, double l) // Note: The returned values of the overloaded methods are the same,
{
System. out. println (\ "a barking dog! \");
This. bark (5, \ "China \");
}
Void bark (int a, String n) // The reload method cannot be distinguished by return values, but can only be distinguished by "parameter type" and "class name"
{
System. out. println (\ "a howling dog \");
}

Public static void main (String [] args)
{
Dog dog = new Dog ();
// Dog. bark (); [Page]
// Dog. bark (\ "male \", \ "yellow \");
// Dog. bark (5, \ "China \");

 


Then let's talk about Overriding)


(1) The polymorphism between the parent class and the Child class, and the function of the parent class is redefined. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). In Java, subclasses can inherit the methods in the parent class without re-writing the same method.

But sometimes the subclass does not want to inherit the parent class from the original method, but wants to make some modifications, which requires method rewriting.

Method override is also called method override.


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

If you need the original method of the parent class, you can use the super keyword, which references the parent class of the current class.


(3) The access modification permission of subclass functions cannot be less than that of the parent class;
The following is an example of rewriting:


Concept: Call the object method mechanism.

 

Insider information about dynamic binding:

1. the compiler checks the object declaration type and method name to obtain all candidate methods. Try to comment out the test of the Base class in the above example, and then the compilation will fail.

 

2. Overload decision-making: the compiler checks the parameter types of method calls and selects the unique one from the preceding candidate methods (implicit type conversion will be involved ).

If the compiler finds more than one or not, the compiler reports an error. Try to comment out test (byte B) of the Base class in the above example. 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
Methods.

 

4. When a program is running and dynamic binding is used to call a method, the virtual machine must call the method version of the object that matches the actual type.

In this example, B points to the actual type of TestOverriding, so B. test (0) calls test of the subclass.

However, the subclass does not overwrite test (byte B), So B. test (byte) 0) calls test (byte B) of the parent class ).

If the parent class (byte B) is commented out, the implicit type in step 2 is converted to int, and the test (int I) of the subclass is finally called ).

 

Summary:

Polymorphism is a feature of object-oriented programming and has nothing to do with methods,
Simply put, the same method can be used to process different input data.
Overload -- different parameter lists (static polymorphism)

When the subclass inherits the same method from the parent class, the input data is the same, but the response must be different from the parent class, you must overwrite the parent class method,

That is, rewrite the method in the subclass-same parameter, different implementations (Dynamic polymorphism)

 

Three major features of OOP: inheritance, polymorphism, and encapsulation.

Public class Base
{
Void test (int I)
{
System. out. print (I );
}
Void test (byte B)
{
System. out. print (B );
}
}
Public class TestOverriding extends Base
{
Void test (int I)
{
I ++;
System. out. println (I );
}
Public static void main (String [] agrs)
{
Base B = new TestOverriding ();
B. test (0)
B. test (byte) 0)
}
}


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

 

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

Public class Father {

Public void speak (){

System. out. println (Father );

}

}

Public class Son extends Father {

Public void speak (){

System. out. println ("son ");

}

}

This is also called polymorphism. The rewriting method can only exist in an inheritance relationship. The rewriting method can only override non-private methods of the parent class.

In this example, when the Father class speak () method is private, the Son class cannot re-write the Father class speak () method. In this case, the Son class speak () the method is equivalent to a speak () method defined in the Son class.

When Father class speak () method 1 is final, no matter whether the method is public, protected, or modified by default, the Son class cannot override the Father class speak () method,

The compiler reports an error when trying to compile the code. Example:

Public class Father {

Final public void speak (){

System. out. println ("Father ");

}

}

Public class Son extends Father {

Public void speak (){

System. out. println ("son ");

}

} // The Compiler reports an error;

 

When the Father class speak () method is modified by default, it can only be in the same package and its subclass is overwritten. If it is not in the same package, it cannot be rewritten.

When the Father class speak () method is protoeted, not only in the same package, but also its subclass is overwritten, and it can be rewritten by the subclass of different packages.

 

Rewrite method rules:

1. The parameter list must be exactly the same as the method to be overwritten. Otherwise, it cannot be called a rewrite but a overload.

2. The return type must always be the same as the return type of the method to be overwritten. Otherwise, it cannot be called an overwrite but an overload.

3. The access modifier must be greater than the access modifier of the method to be overwritten (public> protected> default> private)

4. The rewrite method cannot throw a new check exception or a broader check exception than the declared method. For example:

A Method of the parent class declares an IOException check. If you override this method, you cannot throw an Exception. You can only throw a subclass Exception of IOException. You can also throw a non-check Exception.

 

The reload rules:

1. Different parameter lists are required;

2. There can be non-scolding return types, as long as the parameter list is different;

3. Different access modifiers are available;

4. Different exceptions can be thrown;

 

The difference between rewriting and overloading is:

Rewriting polymorphism works. Calling methods that have been overloaded can greatly reduce the input of code. Passing different parameters to the same method name can have different functions or return values.

A clear and concise class can be designed with good rewriting and overloading. It can be said that rewriting and overloading play an extraordinary role in coding.

Notes for learning Java !!!
If you have any questions or want to obtain learning resources during the learning process, join the Java learning exchange group: 159610322 let's learn Java together!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.