Introduction to JavaSE 15: Java object-oriented J rewrite (Override) and Overload (Overload)

Source: Internet
Author: User
Tags c constructor

Introduction to JavaSE 15: Java object-oriented J rewrite (Override) and Overload (Overload)
Override)

If the subclass is not satisfied with the method that inherits the parent class, you can override the method that inherits the parent class. Override is the implementation of subclass methods that allow access to the parent class.

The process is rewritten. Neither the return value nor the form parameter can be changed. That is, the shell remains unchanged and the core is overwritten. When a method is called, The subclass method is called first.

Syntax Rules:

A return value type

Method B

C parameter type and quantity

It must be the same as the method inherited by the parent class.

The advantage of rewriting is that child classes can define their own behaviors as needed. That is to say, sub-classes can implement the parent class method as needed. Face-to-face

In the object principle, rewriting means that any existing method can be rewritten.

Instance:

Test. java source file code:

Class Animal {// method of the Animal class public void move () {System. out. println ("animals can be moved");} class Dog extends Animal {// override the move () method of the parent class Animal class public void move () {System. out. println ("dogs can run and walk");} public class Test {public static void main (String args []) {Animal a = new Animal (); // Animal object Animal B = new Dog (); // Dog object // Method for executing the Animal Class. move (); // Method B for executing the Dog class. move ();}}

The above example compilation and running results are as follows:

In the above example, although B belongs to the Animal type, it runs the move () method of the Dog class. This is because in the compilation phase,

Only check the reference type of the parameter. However, during runtime, the Java Virtual Machine (JVM) specifies the object type and runs the object's method. Therefore

In this example, the reason for successful compilation is that the Animal class has the move () method. However, during runtime, the method of a specific object is run.

Consider the following example:

Test. java source file code:

Class Animal {// method of the Animal class public void move () {System. out. println ("animals can be moved");} class Dog extends Animal {// override the move () method of the parent class Animal class public void move () {System. out. println ("dogs can run and walk");} // The bark method public void bark () {System. out. println ("dogs can bark") ;}} public class Test {public static void main (String args []) {Animal a = new Animal (); // Animal object Animal B = new Dog (); // Dog object // Method for executing the Animal Class. move (); // Method B for executing the Dog class. move (); B. bark ();}}

The above example compilation and running results are as follows:

This program will throw a compilation error because the reference type Animal of B does not have the bark () method.

Note:

The A parameter list must be exactly the same as the method to be overwritten;

B. The return type must be exactly the same as the return type of the method to be overwritten;

C. The access permission cannot be higher than that of the method rewritten in the parent class. For example, if a method of the parent class is declared as public

If you override this method, it cannot be declared as protected.

D. The member method of the parent class can only be overwritten by its subclass.

Methods declared as final by E cannot be overwritten.

The method declared as static by F cannot be overwritten, but can be declared again.

The G subclass and the parent class are in the same package. Then, the subclass can override all the methods of the parent class except the methods declared as private and final.

The H subclass and the parent class are not in the same package. Therefore, the subclass can only override the non-final method declared as public and protected for the parent class.

The I-Rewrite method can throw any non-forced exception, regardless of whether the method to be overwritten throws an exception. However, the override method cannot throw a new mandatory

Exception, or a wider range of mandatory exceptions than the declared override method, and vice versa.

The J constructor cannot be rewritten.

K. If you cannot inherit a method, you cannot override this method.

Use of Super keywords

When the override method of the parent class needs to be called in the subclass, the super keyword is used. Internal use of the object can represent the parent class object.

Access the attributes of the parent class: super. age;

Method for accessing the parent class: super. eat ();

Usage of super:

The constructor of its parent class must be called during the constructor of subclass.

B. If the constructor of the Child class does not display the constructor of the parent class, the system calls the constructor of the parent class without parameters by default.

C. If the constructor is explicitly called, it must be in the first line of the constructor of the subclass,

D. If the constructor of the parent class is not explicitly called in the constructor of the subclass, and the parent class does not have a constructor without parameters, an error occurs during editing.

Instance:

Class Animal {public void move () {System. out. println ("animals can be moved");} class Dog extends Animal {public void move () {super. move (); // apply the super class method System. out. println ("dogs can run and walk");} public class Test {public static void main (String args []) {Animal B = new Dog (); // Dog object B. move (); // Method for executing the Dog class }}

The above example compilation and running results are as follows:

Overload)

Overloading is a class with the same method name and different parameters. The return types can be the same or different. Each overloaded party

Each method must have a unique list of parameter types. The overload of the constructor is only a special case of the overload method.

Reload rules:

A. the parameter list must be changed for the overloaded method;

The method that B is overloaded can change the return type;

The method that C is overloaded can change the access modifier;

D. The overloaded method can declare a new or wider check exception;

E method can be overloaded in the same class or in a subclass.

Reload rules of constructor:

The A constructor is the same as the class name.

The B constructor does not return values.

The number of parameters in the C constructor list is different.

D. The order of the constructor parameter list is different.

E. The constructor automatically selects the corresponding method based on different parameters when calling the constructor.

Normal method overload instance:

Overloading. java source 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 have different types: public String test (int a, String s) {System. out. println ("test3"); return "returntest3";} public String test (String s, 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 ));}}
Running result:

Constructor overload instance:

Test. java source file code:

Public class Test {public int a; public float B; public Test () {System. out. println ("I Am a construction method without Parameters");} public Test (int a, float B) {this. a = a; this. B = B; System. out. println ("constructor with Parameters");} public static void main (String [] args) {Test t1 = new Test (); test t2 = new Test (1, 2F );}}

Running result:

Iii. Differences between rewriting and overloading:

Related Article

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.