overriding and hiding methods and variables in Java in Inheritance (i)

Source: Internet
Author: User

We know that in Java, subclasses can inherit the parent class, and if the subclass declares a method that has the same name as the parent class, we all know that if you rewrite it, but in fact it is divided into two cases, that is, methods and variables in the inheritance of the cover and hide problems, these conceptual things seem boring, But in the interview or SCJP certification questions around these are often encountered, so here to discuss

First, let's look at some concepts.

Class variables: Also called static variables, which belong to a class and can be accessed by class names.

Instance variable: an instance of a class, an object that can access an instance variable through an object, but cannot access an instance variable through the class name.

static method: Accessed through the class name.

Instance method: Object access, which cannot be accessed through the class name.

Hidden : Child hides the parent's variables and methods, then child cannot access the parent's hidden variables or methods, but converts B to a to access a hidden variable or method

Overwrite : Child overrides the parent's variable or method, then child cannot access the variable or method that the parent is overridden, and the parent's overridden variable or method is also inaccessible after the child is converted to parent

First look at the rules for overriding and hiding methods and variables in Java when they inherit

1. Instance and static variables of the parent class can be hidden by a variable of the same name in the quilt class

2. Static method of the parent class the quilt class with the same name static method hidden

3. Instance methods of the parent class overridden by an instance variable of the same name in the quilt class

There are a few more things to note.

1. You cannot use a static method of a subclass to hide An instance method that is equally marked in the parent class (that is, the return value name parameter is the same)

2. You cannot overwrite a static method that is equally marked in the parent class with an instance method of the child class

3, the variable will only be hidden will not be overwritten , whether he is an instance variable or a static variable, and the child class static variable can hide the parent class instance variable, the child class's instance variable can hide the parent class static variable

4. The final method (the method with the keyword final) cannot be overwritten.

O (∩_∩) o haha ~ is not a bit around the mouth, okay we look at an example

Create a class of two parent-child class relationships

  1. Parent class
  2. Class Parent
  3. {
  4. public static String kind="Javastudy.extendsstudy.parent";
  5. public static int age=50;
  6. Public String name="Parent";
  7. //static method, return package name
  8. public static String Getkind ()
  9. {
  10. System.out.println ("The Getkind () method of the parent has been called");
  11. return kind;
  12. }
  13. //static method, return age
  14. public static int getage ()
  15. {
  16. System.out.println ("The Getage () method of the parent has been called");
  17. return age;
  18. }
  19. //instance method, return name
  20. Public String getName ()
  21. {
  22. System.out.println ("The GetName () method of the parent has been called");
  23. return this.name;
  24. }
  25. }
  26. Sub-class
  27. Class child extends Parent
  28. {
  29. public static String kind="Javastudy.extendsstudy.child";
  30. public int age=25;
  31. Public String name="Child";
  32. //Hide Parent class static method
  33. public static String Getkind ()
  34. {
  35. System.out.println ("Child's Getkind () method was called");
  36. return kind;
  37. }
  38. //Get parent class package name
  39. public static String Getparentkind ()
  40. {
  41. return parent.kind;
  42. }
  43. //Override Parent class instance method
  44. Public String getName ()
  45. {
  46. System.out.println ("Child's GetName () was called");
  47. return this.name;
  48. }
  49. //Get parent class name
  50. Public String getparentname ()
  51. {
  52. return Super.name;
  53. }
  54. /* 
  55. * Error, instance method cannot overwrite static method of parent class
  56. public int Getage ()
  57. {
  58. return this.age;
  59. }
  60. */
  61. }

And then the test.

  1. Class Test
  2. {
  3. public static void Main (string[] args)
  4. {
  5. Child child=New Child ();
  6. System.out.printf ("Subclass name:%s, Age:%d, package name:%s%n", child.name,child.age,child.kind);
  7. //output: Subclass Name: Child, Age: 25, Package: Javastudy.extendsstudy.child
  8. //Convert child to Parent object
  9. Parent Parent=child;
  10. System.out.printf ("converted name:%s, Age:%d, package name:%s%n", parent.name,parent.age,parent.kind);
  11. //output: Converted Name: Parent, Age: 50, Package: Javastudy.extendsstudy.parent
  12. System.out.printf ("subclass access to the parent class is hidden by the instance variable name:%s%n", Child.getparentname ());
  13. //Output: Subclass accesses the instance variable that the parent class is hidden name:parent
  14. System.out.printf ("subclass access to the parent class is hidden static variable kind:%s", Child.getparentkind ());
  15. //Output: Subclass accesses a static variable that is hidden by the parent class Kind:javastudy.extendsstudy.parent
  16. Child.getname ();
  17. //output: Child's GetName () is called
  18. //************** Pay attention to this method, return is the subclass of the GetName
  19. Parent.getname ();
  20. //output: Child's GetName () is called
  21. Child.getkind ();
  22. //output: Child's Getkind () method is called
  23. Parent.getkind ();
  24. //output: The Getkind () method of the parent is called
  25. }
  26. }

All right, I'll summarize it after we've seen the results.

1. Instance methods with the same name are overwritten , static methods with the same name are hidden , and the GetName instance method of the child class overrides the GetName instance method of the parent, Chind Getkind method Hides the Getkind method of the parent class

2. The difference between hiding and overriding is that when a subclass object is converted to a parent class object, it is able to access the hidden variables and methods of the parent class, and cannot access the method that the parent class is overridden

3. If you need to access an instance variable that the parent class is hidden , plus super, such as accessing the name of the parent class, write Super.name.

4. Subclasses to access the class variables that are hidden by the parent class, you need the name of the parent class plus "." To access. For example: Parent.name

This article is not to teach you to rewrite the parent class variables, but focus on the methods and variables of the hidden and covered, these small knowledge points although in the project is not how to use, but must remember, otherwise many errors will be puzzled, there is scjp the question bank of this kind of problem greatly exists, So I think these small knowledge is still remembered as good.

overriding and hiding methods and variables in Java in Inheritance (i)

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.