In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly talks about "what is the difference between Override and Overload, which is a stumbling block to getting started with Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what is the difference between Override and Overload, which is a stumbling block to the introduction of Java.
1. Method rewriting (Override) 1. Basic concepts of method rewriting
Rewriting is that the subclass rewrites the implementation of the method that the parent class is allowed to access, and the return value and formal parameters cannot be changed. That is, the shell remains unchanged and the core is rewritten!
The advantage of rewriting is that subclasses can define their own specific behavior as needed. In other words, the subclass can implement the methods of the parent class as needed.
An overridden method cannot throw a new check exception or an exception that is broader than the overridden method declaration.
Example:
Public class Animal {public void eat () {System.out.println ("Animals can eat");}} / / Dogs inherit the animal class Dog extends Animal {/ / rewrite method, which requires the Override note @ Override / / the method body remains unchanged, and the method body changes public void eat () {System.out.println ("can eat"). }} class Test {public static void main (String [] args) {/ / create animal instance object Animal a = new Animal (); / / create dog instance object Animal d = new Dog (); / / execute animal method a.eat (); / / execute dog method d.eat ();}}
Compilation result:
Animals can eat.
Dogs can eat.
As you can see in the above example, although d is of type Animal, it runs the eat method of the Dog class. This is because during the compilation phase, you only check the reference type of the parameter. At run time, however, the Java virtual machine (JVM) specifies the type of object and runs the method of the object. So in the above example, the compilation is successful because there are eat methods in the Animal class, but at run time, you are running object-specific methods.
If there is no specific method in the parent class, the program will report an error. The compilation was not successful.
Public class Animal {public void eat () {System.out.println ("Animals can eat");}} / / Dogs inherit the animal class Dog extends Animal {/ / rewrite method, which requires the Override note @ Override / / the method body remains unchanged, and the method body changes public void eat () {System.out.println ("Dogs can eat") } public void look () {System.out.println ("dogs can see");}} class Test {public static void main (String [] args) {/ / create animal instance object Animal a = new Animal (); / / create dog instance object Animal d = new Dog (); / / execute animal method a.eat () / / execute dog method d.eat (); d.look (); / / this statement cannot compile}} 2. Method rewriting basic rules and precautions
The parameter list must be exactly the same as the parameter list of the overridden method.
The return type can be different from the return type of the overridden method, but it must be a derived class of the return value of the parent class (the return type of java5 and earlier versions can be the same, java7 and later can be different).
Access cannot be lower than that of a method overridden in the parent class. For example, if a method of the parent class is declared as public, then overriding the method in the subclass cannot be declared as protected.
The member method of a parent class can only be overridden by its subclasses.
Methods declared as final cannot be overridden.
Methods declared as static cannot be overridden, but can be declared again.
If the subclass and the parent class are in the same package, then the subclass can override all the methods of the parent class, except those declared as private and final.
If the subclass and the parent class are not in the same package, the subclass can only override the non-final methods declared as public and protected of the parent class.
An overridden method can throw any unforced exception, regardless of whether the overridden method throws an exception or not. However, an overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than that declared by the rewritten method, or vice versa.
Constructors cannot be overridden.
If you cannot inherit a class, you cannot override the methods of that class.
2. Overload method overloading 1. What is overloading
Overloading (overloading) is in a class with the same method name but different parameters. The return type can be the same or different. Each overloaded method (or constructor) must have a unique list of parameter types. The most common place is the overloading of constructors.
2. Rules for overloading
The overloaded method must change the parameter list (the number or type of parameters is different)
Overloaded methods can change the return type
Overloaded methods can change access modifiers
Overloaded methods can declare new or broader check exceptions
Methods can be overloaded in the same class or in a subclass.
The return value type cannot be used as a distinguishing criterion for overloaded functions.
Example:
/ * Student class * / class Student {private String name; private int age; public Student () {} public Student (String name) {this.name = name;} public Student (int age) {this.age = age;} public Student (String name,int age) {this.name = name; this.age = age } public void show () {System.out.println (name + "," + age);} / * Test class * / public class StudentDemo {public static void main (String [] args) {/ / create object Student S1 = new Student (); s1.show (); / / public Student (String name) Student S2 = new Student ("Lin Qingxia") S2.show (); / public Student (int age) Student S3 = new Student (30); s3.show (); / public Student (String name,int age) Student S4 = new Student ("Lin Qingxia", 30); s4.show ();}}
The three methods in the example constitute overloading.
Distinguishing point overloading method rewriting method parameter list must not be modified, return type must not be modified, exceptions can be modified, exceptions can be reduced or deleted, new or wider exception access can be modified, more stringent restrictions cannot be made (restrictions can be reduced) 3. Summary:
Method rewriting (Overriding) and overloading (Overloading) are different manifestations of java polymorphism. Rewriting is a manifestation of polymorphism between parent and child classes, and overloading can be understood as a concrete form of polymorphism.
(1) method overloading is called method overloading (Overloading) when multiple methods with the same name are defined in a class, but the number of their parameters is different or the number of parameters is the same, but the type and order are different.
(2) method rewriting is a method in which the name of the method is the same as that of the parent class in the subclass, and the number of parameters is the same as the type, and the return value is the same, which is called rewriting (Overriding).
(3) method overloading is the polymorphism of a class, while method rewriting is a polymorphism of subclass and parent class.
Answer to the interview questions:
The rewriting Overriding and overloading Overloading of the method are different manifestations of Java polymorphism. Rewriting Overriding is a manifestation of polymorphism between parent and child classes, and overloading Overloading is a manifestation of polymorphism in a class. If a method is defined in a subclass with the same name and parameters as its parent class, we say that the method is Overriding. When an object of a subclass uses this method, the definition in the subclass is called, and for it, the definition in the parent class seems to be "masked". If multiple methods of the same name are defined in a class, they either have a different number of parameters or have different parameter types, it is called Overloading of the method. The method of Overloaded is that you can change the type of return value.
Here is a picture to sum up to help you understand
At this point, I believe that everyone has a deeper understanding of "what is the difference between Override and Overload, which is a stumbling block to Java entry?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.