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 879129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:52:49+00:00 2026-05-15T11:52:49+00:00

I created instance of base class in derived class and tried to access protected

  • 0

I created instance of base class in derived class and tried to access protected members.

I can directly access protected members in a derived class without instantiating the base class.

Base class:

package com.core;

public class MyCollection {

      protected Integer intg;
}

A derived class in the same package –

package com.core;

public class MyCollection3 extends MyCollection { 

 public void test(){

  MyCollection mc = new MyCollection();
  mc.intg=1; // Works
 }
}

A derived class in a different package –

package secondary;

import com.core.MyCollection;

public class MyCollection2 extends MyCollection{ 

 public void test(){
  MyCollection mc = new MyCollection();
  mc.intg = 1; //!!! compile time error - change visibility of "intg" to protected
 }
}

How it is possible to access a protected member of a base class in a derived class using instance of base class when derived class is also in same package but not when derived class is in different package?

If I mark protected member as “static” then I am able to access protected member of base class using instance of base class in a derived class which resides in a different package.

  • 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-05-15T11:52:50+00:00Added an answer on May 15, 2026 at 11:52 am

    You’re right that you can’t do this. The reason why you can’t access the field, is that you’re not in the same package as the class, nor are you accessing an inherited member of the same class.

    The last point is the critical one – if you’d written

    MyCollection2 mc = new MyCollection2();
    mc.intg = 1;
    

    then this would work, as you’re changing a protected member of your own class (which is present in that class through inheritance). However, in your case you’re trying to change a protected member of a different class in a different package. Thus it should come as no surprise that you’re denied access.

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

Sidebar

Related Questions

I have the following models: class Instance < ActiveRecord::Base has_many :users has_many :books end
I want a class (called PremiseServer) in my Mvvm-Light solution (WP7) to subscribe to
I have a class Wizard which creates a wizard with pages defined in the
I am having issues while passing objects across Appdomains.During further investigation I found that
This is a design question, assuming C++ and a reference counted object hierarchy. A
I did look at the related posts that were answered, but still get the
I have encountered the following problem which proved to me that I know far
According to Storing C++ template function definitions in a .CPP file it is easy
The title may be not exactly, but I don't know how to express it.
I would like to start my question by stating that this is a C++

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.