A long journey of life: Learning Jakarta basics-rewriting (overwrite), heavy load, and life-path jakarta

Source: Internet
Author: User

A long journey of life: Learning Jakarta basics-rewriting (overwrite), heavy load, and life-path jakarta

First, we are now entering the Jakarta era, from the original Oracle master to the Eclipse foundation, but do not want to discard the java name, so the foundation reselected Jakarta EE (Jakarta ). However, we understand the principle of changing the soup without changing the dressing, and the foundation is the same.

Concepts

Override: overwrite (= overwrite) --> generally, when a subclass inherits the parent class, it overwrites the method of the parent class to implement different functions. The method to override a subclass has a vertical relationship with the name, return type, and parameter of the method corresponding to the parent class.

Overload: Reload --> the relationship between methods in the same class. These methods have the same name but different parameter forms, so there is a horizontal relationship. The overload method is used to select a method based on the call time parameter table, the form parameter table, and the parameter Order (different parameter types. To put it simply, method Overloading is the multiple implementation methods of the same function of the class. which method is used depends on the parameters provided by the caller.

Rules that are reloaded by rules:

1. The method name must be the same and the parameter format is different (the type, number, and order of the parameters are at least one item is different ).

1 package bank; 2 3/** 4 * Created by zjc on 2018/3/8. 5 */6 public class overload 7 {8 public float getCount () {9 return 0; 10} 11 public int getCount (int a, int B) {12 return a + B; 13} 14 public float getCount (float a, float B) {15 return a + B; 16} 17 public float getCount (double c, float d) {18 return (float) c + d; 19} 20 private float getCount (float d, double c) {21 return (float) c + d; 22} 23 public static void main (String [] args) {24 overload o = new overload (); 25 System. out. println (o. getCount (2.3f, 2.4f); 26} 27}View Code

Under this rule: The return types of methods can be different, and the modifiers of methods can be different.

When the parameter types are the same, there is no difference between the order of parameters, and it cannot constitute a heavy load.

     

 

2. You cannot use the access permission, return value type, or throw an exception for heavy load (if Rule 1 does not meet the requirements );

2.1 if only the return type is different: the overload cannot be formed.

     

2.2 If only the access control modifier is different: the overload cannot be formed.

     

 

3. The method exception type and quantity do not affect the heavy load;

Rewrite Rules

1. Rewrite (subclass) and the return type, parameter, and method name of the overwritten (parent class) method must be the same; otherwise, an error occurs during compilation.

1/** 2 * Created by zjc on 2018/3/8. 3 */4 public class override {5 public static void main (String [] args) {6 // create a Cat 7 cat Cat = new cat (); 8 Cat. cry (); 9 Dog dog = new Dog (); 10 dog. cry (); 11} 12} 13 class Animal {14 int age; 15 String name; 16 17 public void cry () {18 System. out. println ("I'm an animal. I don't know how to call it! "); 19} 20} 21 // Garfield 22 class Cat extends Animal {23 // override parent class 24 public void cry () {25 System. out. println ("Meow! "); 26} 27} 28 // glasses Dog 29 class Dog extends Animal {30 // overwrite parent class 31 public void cry () {32 System. out. println ("trademanager! "); 33} 34}View Code

2. Override (subclass) cannot reduce the access permission (public> protected> default> private) of the override (parent class) method ).

The parent class private subclass public can be rewritten in this way.

3. The rewrite (subclass) method must be consistent with the exception thrown by the override (parent class) method, or its subclass.

4. The method to be overwritten (parent class) cannot be private. Otherwise, the subclass can only define new methods and cannot be overwritten.

5. Static methods cannot be rewritten as non-static methods (compilation error ).

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.