Protected what do you mean?

Source: Internet
Author: User

Now that we have understood the concept of inheritance, protected this keyword finally has meaning. In an ideal situation, private members are "private" at any time, and no one is allowed to visit them. In practice, however, it is often possible to hide something deeply, but at the same time allow access to the members of the derived class. The protected keyword can help us do this. It means "it is private, but it can be accessed by anything inherited from this class or anything else in the same package." In other words, protected in Java can become "friendly".
The best we can do is to maintain the private state of the members-and, in any case, retain the right to modify the implementation details of the foundation. In this context, the protected method allows the inheritors of a class to have controlled access:

: Orc.java
//The Protected keyword
import java.util.*;

Class Villain {
  private int i;
  protected int Read () {return i;}
  protected void Set (int ii) {i = II}
  Public villain (int ii) {i = II}
  public int value (int m) {return m*i}
}

public class Orc extends villain {
  private int J;
  Public ORC (int JJ) {super (JJ); j = JJ;}
  public void change (int x) {set (x);}
} ///:~

As you can see, the change () has access to set () because its properties are protected (protected).

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.