"Java keyword-interface" Why are properties and methods in an interface default to public

Source: Internet
Author: User

Assuming that the method or property defaults to protected, its subclasses can also be protected, and there may be situations where these properties and methods can cause errors to be accessed properly. The interface does not know which class will be called in the future, so minimize the access control of the interface and set the permissions to public. With this in mind, Sun began to stipulate that interface properties and methods are public by default.

Example: If the protected String getName () is declared in the IPerson interface, the person implements the GetName () interface of IPerson

 PackagePerson Public  interface IPerson {    protectedStringGetName();} PackagePerson Public  class  person implements IPerson{    protectedString name ="Liuzhibo";protectedStringGetName(){returnName }} PackageMan Public  class  man {     Public Static void Main(String [] args) {Person person =NewPerson (); Person.name;//name modifier changed to public to accessPerson.getname ();//getname modifier changed to public to access}}

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

"Java keyword-interface" Why are properties and methods in an interface default to public

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.