The protected in Java

Source: Internet
Author: User
Tags class definition constructor inheritance

The protected (protected) Access indicator requires early recognition. The first thing to note is the fact that it is not necessary to understand the content of this section until you continue to study the book until you have inherited that chapter. But in order to maintain the integrity of the content, here is still a brief description of this, and provide relevant examples.
The protected keyword introduces us to a concept called "inheritance". It builds on existing classes and adds new members to them without affecting existing classes-we call this existing class "base class" or "Basic Class" (base Class). Can also change the behavior of existing members of that class. For inheritance from an existing class, we say our new class "extension" (extends) has the existing class. As shown below:
Class Foo extends Bar {
The rest of the class definition looks exactly the same.
If you create a new package and inherit from a class within another package, the only member you can access is the public member of the original package. Of course, if you inherit in the same package, the inherited package will have access to all "friendly" members. Sometimes, the creator of the underlying class prefers to provide a special member and allow access to the derived class. This is protected's job. If you refer back to the Cookie.java file in the 5.2.2 section, "Public: Interface Access," The following class does not have access to "friendly" Members:

: Chocolatechip.java
//Can ' t access friendly member
//In another class
import c05.dessert.*;

public class Chocolatechip extends cookies {public
  chocolatechip () {
   System.out.println (
     "chocolatechip Constructor ");
  }
  public static void Main (string[] args) {
    chocolatechip x = new Chocolatechip ();
    //! X.foo (); Can ' t access foo
  }
}///:~

One interesting thing to note about inheritance is that if Method foo () exists in the class cookie, it will also exist in all classes that inherit from the cookie. But because Foo () is "friendly" in the outer package, we can't use it. Of course, it can be turned into public. But in this way, because everyone is free to access it, it may not be the situation we want. Modify the class cookie as follows:

public class Cookie {public
  cookie () { 
    System.out.println ("Cookie constructor");
  }
  protected void foo () {
    System.out.println ("foo"); 
  }

You can still access foo () "friendly" in the package dessert, but other things inherited from cookies are also freely accessible. However, it is not 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.