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

  • SEARCH
  • Home
  • 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 8334581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:24:00+00:00 2026-06-09T03:24:00+00:00

I am learning about access levels in java and I have created 3 classes:

  • 0

I am learning about access levels in java and I have created 3 classes:
In package my.inheritance
I have class A and class C

package my.inheritance;
public class A {
protected int a=15; 
}

package my.inheritance;
public class C {

public static void main(String[] args)
{
    A a = new A();
    System.out.println(a.a);
}
}

And in another package called my.inheritance.test I have a class B trying to access protected field of int value a but the compiler complains for this!

package my.inheritance.test;
import my.inheritance.A;
public class B extends A{

public static void main(String[] args)
{
    A a = new A();
    int value = a.a;
    System.out.println(value);
}
}

I was under the impression with protected you can access a member from a different class in a different package as long as you subclass it! Why the visibility error then ?

  • 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. Editorial Team
    Editorial Team
    2026-06-09T03:24:01+00:00Added an answer on June 9, 2026 at 3:24 am

    Every method can access protected fields of its own class, and all its parent classes. This does not include access to protected fields of another class, even if they have the corresponding base class in common.

    So methods in class B can access protected fields from objects of class B, even if they were declared in A, but not from some other object of class A.

    One could say that class B inherits the protected members from A, so now every B has those members as well. It doesn’t inherit access to the protected members of A itself, so it cannot operate on protected members of any A but only on those of B, even if they were inherited from A.

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

Sidebar

Related Questions

We just started learning about class inheritance and attribute lookup in python. I have
I'm currently learning about class inheritance in my Java course and I don't understand
I am learning about the Runtime class in java, and am testing the use
I'm learning about accessor methods and enumeration. I wrote a public class 'Car' under
I have been learning about Delegation and Data Sources for iOS programming and need
I'm currently learning about pointers in my C++ Algorithms class and while I understand
I have been learning about indexes in Oracle SQL, and I wanted to conduct
So I have been working with multiple different MS Access apps at work (about
I'm learning about Java EE (EE 6 specifically) and find the differences between EJB
I'm learning Zend Framework, but I have some doubts about the usage and the

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.