Meet java-5.3 Public private protected from scratch

Source: Internet
Author: User
Tags throw exception

In this chapter we discuss public, private, and protected.

1.class_path

environment variable inside the Class_path, this path is our Java to find the path of the file.

Note: "." is usually added to the Class_path. Represents the current folder, if you do not add this symbol, then you can edit the Java is likely not compile, because in the class_path within a certain number of folders within the very likely to find your source file.


2.public

For all members exposed

Package Com.ray.ch05;public class Test {private int id;private void Say () {}public static void main (string[] args) {new Te St ();}}


For the above code, the other classes can only access the constructor, because the constructor is public and the others are private.


3.private

can only be accessed in this category, all others are not.

Package Com.ray.ch05;public class Test {private int id;private void Say () {}public static void main (string[] args) {new Te St (). Say ();//access Private method. New test (). ID = 0;//Sets the value of the private domain System.out.println (new test (). ID);//Read the value of the private domain}}

We build another class in the same package Test2

Package Com.ray.ch05;public class Test2 {public static void main (string[] args) {new test ();//New Test (). Say ();//Throw exception//n EW test (). id = 0;//Throw exception//System.out.println (New Test (). ID);//Throw Exception}}

The above class can only be New Test () and the other operations are wrong.


4.protected

Let's modify the two classes just now so that Test2 inherits Test and then builds a protected domain in test.

Package Com.ray.ch05;public class Test {private int id;protected String name;private void Say () {}public static void main ( String[] (args) {new Test (). Say ();//access Private method. New test (). ID = 0;//Sets the value of the private domain System.out.println (new test (). ID);//Read the value of the private domain}}

Package Com.ray.ch05;public class Test2 extends Test {public static void main (string[] args) {new test (); System.out.println (New Test2 (). name);//Read the parent class's Domain//System.out.println (New Test2 (). ID);//throw exception because ID is private}}

As can be seen from the above code, the subclass Test2 can read the protected property of the parent class test. Similarly, it can be extended to the method above.

Package Com.ray.ch05;public class Test {private int id;protected string name;private void Say () {}protected string setName () {return "";} public static void Main (string[] args) {new Test (). Say ();//access Private method. New test (). ID = 0;//Sets the value of the private domain System.out.println (new test (). ID);//Read the value of the private domain}}

Package Com.ray.ch05;public class Test2 extends Test {public static void main (string[] args) {new test (); System.out.println (New Test2 (). name);//Read the parent class's Domain System.out.println (new Test2 (). SetName ());//The method of reading the parent class// System.out.println (New Test2 (). ID);//throw exception because ID is private}}


Summary: This chapter focuses on some of the features of public private protected.


This chapter is here, thank you.

-----------------------------------

Directory


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Meet java-5.3 Public private protected from scratch

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.