The cover and hide generalization of Java class members (member variables and methods)

Source: Internet
Author: User
Tags modifier

Previously know rewrite override and overload overload difference, usually also catch use is, eclipse error again.

Recently read a book in the rewrite, replace, overwrite, replace, hide, reload, simply disorderly, summed up a bit.

from the overall point of view, Java only rewrite, hide, overload 3 kinds, specifically summarized as follows:

Basic concepts:

Method Signature: Method name + parameter list (parameter type, number, order).
================================= "overriding" =================================

Some books are called overlays, replacements, and replacements. This article is called rewriting.

Only instance methods can be overridden! The overridden method must still be an instance method! (instance method------ override ------> instance method)
Both member variables and static methods cannot be overridden and can only be hidden.

overrides an instance method: There is an instance method A in the superclass parent, a subclass child defines an instance method B with the same signature and subset return type as a, and the subclass object Childobj can only invoke its own instance method B.
                               even if you convert a subclass object Childobj to a superclass object Parentobj,parentobj still only invoke the overridden instance method B!
                               (instance method A in superclass object parentobj has been overridden by instance method b )

       

The

overrides the following syntax rules:
(1) The method signature must be the same (parameter type, number, order);
(2) The return type is required, in 2 cases:
     A. The return type of the overridden method is the base type: The return type of the overridden method must be the same. The
                basic types include (Byte, Short,int,long,float,double,char,boolean, in fact, also includes a void type), but be aware that the return type belongs to the following case when the class is encapsulated: B.
     B. The return type of the overridden method is a reference type: The return type of the overridden method should be "same" or its "subtype";
                 reference types include all types of non-primitive types (that is, class types), such as arrays, strings, and so on. The
(3) Override method's access permission cannot be less than the access permission of the overridden method, which can be more extensive. If the overridden method is a package access permission, the overriding method is public access. The
     override method can change other method modifiers, such as final, synchronized, native, strictfp.
     Regardless of whether the overridden method has a final decorated parameter, the overriding method can increment, retain, and remove the final modifier of the parameter (the parameter modifier is not a method signature). The
(4) Override method throws an exception range that cannot be greater than the scope of the exception thrown by the overridden method (or it can not throw an exception).
(5) cannot override the final method. (The meaning of the final modifier is to prevent any subclasses from overriding the method.)
(6) cannot override static static methods. (Can be written in form, but not in nature, belong to the hidden below)
(7) If a method cannot be inherited, it cannot be overridden. Or, it can be overridden only if the method can be accessed.
     is typically the private method of the superclass.

for (3) need to note:
1. Overriding overrides is required for the return type, while the overloaded overload does not require a return type.
Overloading can change the return type. Because the compiler can statically compile different methods by identifying the signature of the method. This is also one of the differences between overloading and rewriting in Java.
2. Strictly speaking, overrides are polymorphic because they are dynamic bindings (or dynamic binders), and overloads are static bindings that can be determined at compile time.

================================="Hide"=================================

Hiding and covering are very similar in form (grammatical rules), but there are essential differences.

Only member variables (whether static or not) and static methods can be hidden.

----------------------------member Variable------------------------

Hidden member Variable: The superclass parent has a member variable A, the subclass child defines a member variable B with the same name as a, and the subclass object Childobj calls its own member variable B.
If you convert a class object Childobj to a superclass object Parentobj,parentobj calls the member variable a of the superclass!
Note: 1. When you hide a member variable, you can change the variable type (regardless of the base type or the hidden type) as long as you have the same name
2. You cannot hide private member variables in a superclass, in other words, you can only hide the member variables that are accessible.
3. When you hide a superclass member variable A, you can reduce or increase access to the subclass member variable B, as long as a is not private
4. The hidden member variable is not static-independent! Static variables can hide instance variables, instancevariablesYou can also hide staticvariables
5. You can hide the final member variable in the superclass.

----------------------------static Method------------------------

The hidden method must still be a static method! (Static methods------ hidden ------> static methods)

Hide static method: Superclass Parent has static method A, subclass child defines static method B with a "same signature and subset return type", and subclass object Childobj calls its own static method B.
If you convert a class object Childobj to a superclass object Parentobj,parentobj call the static method A of the superclass!

The syntax rules for hiding superclass static methods are almost identical to the rules for overriding instance methods, and you only need to modify paragraph (6) to: (6) You cannot hide instance methods.

Example code:

[Java]View PlainCopy
  1. Class Animal {
  2. char haircolor=' B ';
  3. int legnumber=2;
  4. static Boolean ishuman=true;
  5. public static void Testclassmethod () {
  6. System.out.println ("the class" + "method in Animal.");
  7. }
  8. public void Testinstancemethod () {
  9. System.out.println ("the instance" + "method in Animal.");
  10. }
  11. }
  12. Public class Cat extends Animal {
  13. static int haircolor=1;
  14. char legnumber=' A ';
  15. double ishuman=3.1415926E5;
  16. public static void Testclassmethod () {
  17. System.out.println ("The class method" + "in Cat.");
  18. }
  19. public void Testinstancemethod () {
  20. System.out.println ("The instance method" + "in Cat.");
  21. }
  22. public static void Main (string[] args) {
  23. System.out.println ("========child class=========");
  24. Cat Mycat = new Cat ();
  25. System.out.println ("mycat.haircolor=" +mycat.haircolor);
  26. System.out.println ("mycat.legnumber=" +mycat.legnumber);
  27. System.out.println ("mycat.ishuman=" +mycat.ishuman);
  28. Mycat.testclassmethod ();
  29. Mycat.testinstancemethod ();
  30. System.out.println ("========child class---> Parent class=========");
  31. Animal myanimal = Mycat;
  32. System.out.println ("========parent class=========");
  33. System.out.println ("myanimal.haircolor=" +myanimal.haircolor);
  34. System.out.println ("myanimal.legnumber=" +myanimal.legnumber);
  35. System.out.println ("myanimal.ishuman=" +myanimal.ishuman);
  36. Myanimal.testclassmethod ();
  37. Myanimal.testinstancemethod ();
  38. }
  39. }

Output Result:

[Java]View PlainCopy
  1. ========child class=========
  2. mycat.haircolor=1
  3. Mycat.legnumber=a
  4. mycat.ishuman=314159.26
  5. The class method in Cat.
  6. The instance method in Cat.
  7. ========child Class---> Parent class=========
  8. ========parent class=========
  9. Myanimal.haircolor=b
  10. myanimal.legnumber=2
  11. Myanimal.ishuman=True
  12. The class method in Animal.
  13. The instance method in Cat.

The cover and hide generalization of Java class members (member variables and methods)

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.