Java tip [004]: Can the final class have the protected attribute or method?

Source: Internet
Author: User

Can the final class have the protected attribute or method? This is a typical tip. If a class is declared as final, it indicates that the class cannot be inherited. If the class cannot be inherited, can it have protected attributes and methods?

The answer is yes. So, what is the access permission of protected?

A protected attribute or method, which can be accessed by classes in the same package or by quilt classes, but now it cannot have subclasses, at this time, protected is actually the same as the default access permission, and becomes accessible to classes in the same package.

CodeAs follows:


Package net. moon. insignificant. finalclass; </P> <p> final class finalclasssuper {<br/> protected void sayhello () {<br/> system. out. println ("Hello, world "); <br/>}</P> <p> public class finalclassdemo {<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> finalclasssuper S = new finalclasssuper (); <br/> S. sayhello (); <br/>}</P> <p >}< br/>

Next article: Java tip [005]: When will the Finalize method be executed?

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: 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.