The member method in Java and the member variable access rights detailed _java

Source: Internet
Author: User
Tags pack

Remember in an interview written question, some interviewers will ask to write specific like pullic these access qualifiers scope. In fact, I did not go to the system to consider the scope of these access qualifiers, especially in the package outside the case, OK, the written test is not.

This is the basic knowledge of Java, but also the value of the company, there is no way, my head can not remember things, then I could only write down these things to facilitate their own restudying, no nonsense, paste code.

The code is as follows:

Package Com.jaovo; Verification of/** *_1_ member variable access rights * Public private protected defaults (default permissions) * Own package own class can access accessible accessible * own Package other classes access inaccessible accessible * Other packages other classes have access to inaccessible inaccessible unreachable * Own package other classes have inheritance relationships accessible access to inaccessible * Other packages have Inheritable relationships access inaccessible accessible *---------------------------------------------------------------------------*_2_ member Method access rights The verification * Public Private protected defaults (default permissions) * Own package own class accessible accessible * access accessible * Own package other classes accessible Ask access to accessible * Other packages other classes access inaccessible inaccessible unreachable * Own package other classes have inheritance relationships access inaccessible accessible * Other packages other classes have inherited relationships that can be accessed inaccessible
Access not accessible/import CN.JAOVO.D;
Import CN.JAOVO.E; public class chengyuan{//public class Chengyuan extends c{//public class Chengyuan extends d{public class Chengyuan E Xtends e{public static void Main (string[] args) {//permission access to a member variable/*1 b = new B ();/own Package other class System.out.println (
    B.I1);
    System.out.println (B.I2); System.out.println (B.I3);I3 can access private System.out.println (B.I4) in B;
    * */*2 Chengyuan ch = new Chengyuan ();//own Pack own class System.out.println (CH.I1);
    System.out.println (CH.I2);
    System.out.println (CH.I3);
    System.out.println (CH.I4);
    * */*3 d = d = new D ()//other package System.out.println (D.I1); System.out.println (D.I2);//i2 is not public in D, it cannot be accessed from an external package System.out.println (D.I3);//i3 can access private system.out in D. println (D.I4);//i4 can access in D protected//*4 Chengyuan ch = new Chengyuan ();//own package other classes have inheritance Chengyuan extends C syst
    Em.out.println (CH.I1);
    System.out.println (CH.I2);
    System.out.println (CH.I3);//i3 can access System.out.println (CH.I4) in B;
    * */*5 Chengyuan ch = new Chengyuan ()//Other package other classes have inheritance Chengyuan extends D System.out.println (CH.I1); System.out.println (CH.I2);//i2 is not public in D; it cannot be accessed from an external package System.out.println (CH.I3);//i3 can access private System.out in D
    . println (CH.I4); */
//======================================================//Two member method access rights/*1 Chengyuan ch = new Chengyuan ();//own Pack own class System.out.println (CH.M1 ());
    System.out.println (ch.m2 ());
    System.out.println (CH.M3 ());  
    System.out.println (CH.M4 ());
    *//*2 B = new B ();//own package other class System.out.println (B.M1 ());
    System.out.println (b.m2 ());
    System.out.println (B.M3 ());//m3 () can access private System.out.println (B.M4 ()) in B;
    *//*3 E = new E ();//Other Package System.out.println (E.M1 ()); System.out.println (e.m2 ());//m2 is not public in e; it cannot be accessed from an external package System.out.println (E.M3 ());//m3 can access private SYSTEM.O in E Ut.println (E.M4 ());
    M4 () can access protected//*4 c = new C () in E, and/or its own package other classes have inheritance Chengyuan extends C System.out.println (C.M1 ());
    System.out.println (c.m2 ());  
    System.out.println (C.M3 ());//m3 () can access System.out.println (C.M4 ()) in C;
    * *//5 Chengyuan ch = new Chengyuan ();
    System.out.println (CH.M1 ()); System.out.println (ch.m2 ());//no symbols found SysteM.out.println (CH.M3 ());//Cannot Find Symbol System.out.println (CH.M4 ()); 
  Class b{//1 member variable public int i1 = 100;
  int i2 = 200;
  private int i3 = 300;
  protected int i4 = 400;
  2 Member method public int M1 () {return 1;}
  int m2 () {return 1;}
  private int m3 () {return 1;}
protected int M4 () {return 1;}} 
  Class c{//1 member variable public int i1 = 100;
  int i2 = 200;
  private int i3 = 300;
  protected int i4 = 400;
  2 Member method public int M1 () {return 1;}
  int m2 () {return 1;}
  private int m3 () {return 1;}
protected int M4 () {return 1;}} ========================================================//d.class files and e.class files in the CN package, in order to conveniently put them here package
Cn.jaovo; 
  public class d{//1 member variable public int i1 = 100;
  int i2 = 200;
  private int i3 = 300;
  protected int i4 = 400;
  2 Member method public int M1 () {return 1;}
  int m2 () {return 1;}
  private int m3 () {return 1;}
protected int M4 () {return 1;}}
-------Package Cn.jaovo; 
  public class e{//1 member variable public int i1 = 100; int i2 = 200;
  private int i3 = 300;
  protected int i4 = 400;
  2 Member method public int M1 () {return 1;}
  int m2 () {return 1;}
  private int m3 () {return 1;} protected int M4 () {return 1;}}

The above code is in Java member method and member variable access rights detailed, I hope you like.

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.