Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between Java rewriting and overloading?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the difference between Java rewriting and reloading". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Rewrite (Override)

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. For example, a method of the parent class declares a check exception IOException, but cannot throw an Exception exception when overriding this method, because Exception is the parent of IOException and can only throw subclass exceptions of IOException.

In object-oriented principles, rewriting means that any existing method can be overridden. Examples are as follows:

TestDog.java file code:

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");}} public class TestDog {public static void main (String args []) {Animal a = new Animal (); / / Animal object Animal b = new Dog (); / / Dog object a.move (); / / execute Animal class method b.move () / / execute the method of Dog class}}

The compilation and running results of the above examples are as follows:

Animals can move.

Dogs can run and walk.

As you can see in the above example, although b is of type Animal, it runs the move 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 move methods in the Animal class, but at run time, you are running object-specific methods.

Consider the following examples:

TestDog.java file code:

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");} public void bark () {System.out.println ("dogs can bark");}} public class TestDog {public static void main (String args []) {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 b.bark ();}}

The compilation and running results of the above examples are as follows:

TestDog.java:30: cannot find symbolsymbol: method bark () location: class Animalb.bark (); ^

The program will throw a compilation error because the reference type Animal of b has no bark method.

Rewriting rules for methods

The parameter list must be exactly the same as that 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 method, you cannot override it.

The use of Super keyword

Use the super keyword when you need to call the overridden method of the parent class in a subclass.

TestDog.java file code:

Class Animal {public void move () {System.out.println ("animals can move");} class Dog extends Animal {public void move () {super.move (); / / apply the super class method System.out.println ("dogs can run and walk");}} public class TestDog {public static void main (String args []) {Animal b = new Dog (); / / Dog object b.move (); / / execute Dog class method}

The compilation and running results of the above examples are as follows:

Animals can move.

Dogs can run and walk.

Overload (Overload)

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.

Overload rules:

Overloaded methods 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

Overloading.java file code:

Public class Overloading {public int test () {System.out.println ("test1"); return 1;} public void test (int a) {System.out.println ("test2");} / / the following two parameters are in different order: public String test (int aline string s) {System.out.println ("test3"); return "returntest3";} public String test (String sfocus int a) {System.out.println ("test4"); return "returntest4" } public static void main (String [] args) {Overloading o = new Overloading (); System.out.println (o.test ()); o.test (1); System.out.println (o.test (1, "test3"); System.out.println (o.test ("test4", 1));}}

The difference between rewriting and overloading

The parameter list must be modified, the return type can not be modified, exceptions can be modified, exceptions can be reduced or deleted, new or wider exception access can be modified, more stringent restrictions can not be made (restrictions can be reduced)

This is the end of the content of "what is the difference between Java rewriting and reloading". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report