Analysis of constructors, and the public, private, protected, final, this, super keywords, protectedsuper

Source: Internet
Author: User

Analysis of constructors, and the public, private, protected, final, this, super keywords, protectedsuper

I feel unfamiliar with many knowledge points when I was a beginner in JAVA. After reading the teaching material many times, I decided to write it down and deepen my impression so that I could come over and review it later. It seems that there may be many words, but the content is very concise.

First, let's look at the usage of this:

 1 package test; 2  3 public class father { 4  5     private String student1 = "xiaoAi "; 6     private String student2 = "jiangQiao "; 7     private int age1 = 25; 8     private int age2 = 26; 9     private int grade1 = 98;10     private int grade2 = 96;11 12     protected father(String name, int age, int grade) {13         String student1 = "occur error1 ";14         String student2 = "occur error2 ";15 16         name = this.student1;17         age = this.age1;18         grade = this.grade1;19         System.out.println(name + "is" + " " + age + " years old and get" + " " + grade);20 21         name = student2;22         age = age2;23         grade = grade2;24         System.out.println(name + "is" + " " + age + " years old and get" + " " + grade);25     }26 27     public static void main(String[] args) {28         new father("cc", 25, 100);29 30     }31 32 }

 

In the father constructor, the initialized variable is assigned a value again. student1 uses this. The function is used to reference external member variables. This is not the case for student2, because in the father function, student2 is re-String and assigned a value. As a local variable, student2 has a higher priority than the member variable. It does not use the this keyword, so the output result

Super:

1 package test; 2 3 public class father {4 5 public String student = "xiaoXiao"; 6 7 protected father () {8 9} 10 11 // test this and the private-defined range. protected12 protected father (String name, int age, int grade) {13 14} 15 16} is given}
 1 package test; 2  3 public class remote extends father { 4  5     String student = "occur error"; 6  7     protected remote(String name, int age, int grade) { 8  9         name = super.student;10         System.out.println("Here is " + name + " *** " + age + " *** " + grade);11 12     }13 14     public static void main(String[] args) {15 16         new remote("xiXi", 25, 100);17 18     }19 }

Student is initialized in the remote class, but it actually has attributes inherited from father. At this time, if we use the constructor 16 rows to create a new one, it will execute 10th rows and print the information ??

 

Right anher is "xiaoXiao" rather than "occur error ". Because the super keyword can obtain open member variables from the parent class, that is, the variable cannot be modified by private.

If you compare the use of this and super to a tool to help people climb the stairs, this can be a one-click, and super can be a two-digit.

Protected:

 1 package test; 2  3 public class father { 4  5     public static void main(String[] args) { 6         Remote rc = new Remote(); 7         rc.println(); 8          9     }10 11 }
 1 package test; 2  3 public class Remote { 4  5     private String exam = "you only have 30 minutes"; 6  7     protected void println() { 8         System.out.println(exam); 9     }10 11 }

It can be called successfully under the same package. Different packages are not allowed. The system will prompt you to change protected to public, even if it is an inherited relationship, it cannot. In this case, protected does play a protection role, but it is better to use private.

Final:

As the name implies, it is a bit "final", so it seems easier to understand that when the final keyword modifies a variable, the variable must be initialized and cannot be assigned a value, that is, how to exit. When it modifies a class, this class cannot be inherited, and it feels like it is shutdown.

Finally, the constructor is actually involved in the previous code. I personally understand how to use constructor to see what your requirements are. For example:

 

 

Here the constructor is father () and father (String, int, int); that is, when you father ff = new father, you can decide whether to enter a parameter in parentheses or what type of parameter to achieve a certain purpose. When you do not write any constructor, the system will default to a non-argument constructor.

I hope every beginner can master these knowledge points as soon as possible and integrate them into the code. To learn more details, I need to spend more efforts to accumulate...

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.