Member methods and member variable access rights in Java

  • 2020-04-01 04:09:51
  • OfStack

Remember that on a written test for an interview, some interviewers ask you to specify the scope of access qualifiers like pullic. In fact, normally I don't think about the scope of these access qualifiers systematically, especially in and out of the package, OK, the written test doesn't work.  

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

The code is as follows:


package com.jaovo;

import cn.jaovo.D;
import cn.jaovo.E;
//public class Chengyuan{
//public class Chengyuan extends C{
//public class Chengyuan extends D{
public class Chengyuan extends E{
  public static void main(String[] args){
  //Permission access to a member variable
  /*1 B b = new B();//Package other classes yourself
    System.out.println( b.i1 );
    System.out.println( b.i2 );
    System.out.println( b.i3 );//I3 can access private in B
    System.out.println( b.i4 );
    */
  /*2 Chengyuan ch = new Chengyuan();//Package your own classes
    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 packages other classes
    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 in D
    System.out.println( d.i4 );//I4 can access protected in D
    */
  /*4 Chengyuan ch = new Chengyuan();//Package other classes yourself There is an inheritance relationship Chengyuan extends C
    System.out.println( ch.i1 );
    System.out.println( ch.i2 );
    System.out.println( ch.i3 );//I3 is accessible in B
    System.out.println( ch.i4 );
    */
  /*5 Chengyuan ch = new Chengyuan();//Other packages have an inheritance relationship 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 in D
    System.out.println( ch.i4 );
    */
//======================================================
  //Access rights for member methods
    /*1 Chengyuan ch = new Chengyuan();//Package your own classes
    System.out.println( ch.m1() );
    System.out.println( ch.m2() );
    System.out.println( ch.m3() );
    System.out.println( ch.m4() );  
    */
    /*2 B b = new B();//Package other classes yourself
    System.out.println( b.m1() );
    System.out.println( b.m2() );
    System.out.println( b.m3() );//M3 () can access private in B
    System.out.println( b.m4() );
    */
    /*3 E e = new E();//Other packages other classes
    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 in E
    System.out.println( e.m4() ); //M4 () can access protected in E
    */
    /*4 C c = new C();//Package other classes yourself There is an inheritance relationship Chengyuan extends C
    System.out.println( c.m1() );
    System.out.println( c.m2() );
    System.out.println( c.m3() );//M3 () can be accessed in C
    System.out.println( c.m4() );  
    */
    //5
    Chengyuan ch = new Chengyuan();
    System.out.println( ch.m1() );
    System.out.println( ch.m2() );//No sign
    System.out.println( ch.m3() );//No sign
    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;}
}
//========================================================
//The D.class file and the E.class file are in the cn package
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 the member method in Java and member variable access rights detailed, hope you like.


Related articles: