Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 114067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:48:06+00:00 2026-05-11T02:48:06+00:00

The keyword protected grants access to classes in the same package and subclasses (

  • 0

The keyword protected grants access to classes in the same package and subclasses (http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html).

Now, every class has java.lang.Object as superclass (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html).

Hence I conclude that every class may access java.lang.Object‘s methods even if they are protected.

Take a look at the following example:

 public class Testclass {   public Object getOne() throws CloneNotSupportedException {     return this.clone();   }   public Object getTwo() throws CloneNotSupportedException {     return ((Object) this).clone();   } } 

While getOne() compiles fine, getTwo() gives

 Testclass.java:6: clone() has protected access in java.lang.Object         return ((Object) this).clone(); 

I neither understand why getTwo() doesn’t compile nor what’s the difference (regarding the access of java.lang.Objects members) with getOne().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:48:07+00:00Added an answer on May 11, 2026 at 2:48 am

    You can only access protected members of a type in a different package if the compile-time type of the expression you’re referencing it through is either your own class or a subclass. (Where ‘your’ class is the class containing the code.) Your own class has to be a subclass of the type which originally declares the method, too.

    Here’s an example; assume that Base is in a different package to all the other classes:

    package first; public class Base {     protected void Foo() {} }  // Yes, each class is really in its own file normally - but treat // all the classes below as being in package 'second'  package second; public class Child extends Base {     public void OtherMethod(Object x)     {         ((Base) x).Foo(); // Invalid: Base is not Child or subclass         ((Child) x).Foo(); // Valid: Child is Child         ((GrandChild) x).Foo(); // Valid: GrandChild is subclass of Child         ((OtherChild) x).Foo(); // Invalid: OtherChild is not Child or subclass     } }  public class GrandChild extends Child {} public class OtherChild extends Base {} 

    In other words, it’s letting you have access to the protected members of ‘objects which are a like you’.

    Details are in section 6.6.2 of the Java Language Specification:

    A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

    6.6.2.1 Access to a protected Member

    Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then: If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S. If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 462k
  • Answers 462k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem is not with the FormField control itself, but… May 16, 2026 at 12:28 am
  • Editorial Team
    Editorial Team added an answer This looks like HTML, not XML. Assuming that is correct,… May 16, 2026 at 12:28 am
  • Editorial Team
    Editorial Team added an answer That happens because you use the raw type Comparable, and… May 16, 2026 at 12:28 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.