Java class access rights and member access rights resolution _java

Source: Internet
Author: User

There is a problem in writing code: The package defines an interface class, another package to implement it, here using the interface isolation and dependency inversion principle, the two packets of dependencies upside down. However, there is a problem where the implementation class is instantiated using the factory pattern, so the implementation class does not want to be exposed to the package, but the implementation class also implements the public interface. So here's the question, if the class is a default type, and the member function is public, what is the access limit?
Implementation validation
1. First define an interface class in a package:

Package mytest Public
  
interface classaccesstest
{
  void GetData ();
  Charseqence getString ();
}

2. Define implementation classes and factories in the implementation package

Package classaccesstest
  
Import mytest. Classaccesstest
  
class Classaccesstestimp implements Classaccesstest
{
  int mA = 0;
  
  Classaccesstestimp (int a)
  {
    MA = A;
  }
  
  public void GetData ()
  {
    System.out.printlin (' The data is     ' + MA);
  }
  
  Public charsequence getString ()
  {return
    ("The data is" + MA);
  }
  

Package classaccesstest
  
Import mytest. Classaccesstest public
  
class Factory
{public
  static clasaccesstest getaccesstest ()
  {
    return new Classaccesstestimp (ten);
  }
}

3. Then use the interface in the original package:

Package mytest
  
Import classacesstest. Factory

//import classaccesstest. Classaccesstestimp public
  
class Testmain
{public
  static void Main (String arg[])
  {
    Factory.getaccesstest (). getString ();

<span style= "White-space:pre" >  </span>//<span style= "font-family:arial, Helvetica, Sans-serif;" >classaccesstest test = </span>new <span style= "font-family:arial, Helvetica, Sans-serif;" >classaccesstestimp (Ten);</span>
  
  }
}

4. Results:
In the Testmain class, the annotated part works correctly, and the commented out section is used to make an error, classaccesstestimp undefined
Conclusion
It is inferred from the above that the access rights of classes in Java can be less than the access rights of members, depending on the inverted and package packaging requirements, can be achieved to meet the requirements.

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.