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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:14:12+00:00 2026-06-12T09:14:12+00:00

I have some scenario like this: public class BaseClass { protected void protectedMethod(OtherObject object)

  • 0

I have some scenario like this:

public class BaseClass {
    protected void protectedMethod(OtherObject object) {
    ...
    }
} 

public class Child1 extends BaseClass {
    @Override
    protected void protectedMethod(OtherObject object) {
        super.protectedMethod(object);
        // Custom Child1 logic
        ...
    }
}

public class Child2 extends BaseClass {
    @Override
    protected void protectedMethod(OtherObject object) {
        super.protectedMethod(object);
        // Custom Child2 logic
        ...
    }
}

Then when i call the “protectedMethod” iterating through an array of “BaseClass” object, compiler gives me protected access error:

OtherObject other = new OtherObject();

BaseClass[] objects = {
    new Child1(),
    new Child2()
}

for (BaseClass object : objects) {
    object.protectedMethod(other); //this line gives me protected access error
}

But if i do the same in a different not polymorphism manner, it works fine.

OtherObject other = new OtherObject();

Child1 child1 = new Child1();
Child2 child2 = new Child2();

child1.protectedAccess(other);
child2.protectedAccess(other);

I don’t get to tell what is the difference between the two ways.

  • 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-12T09:14:13+00:00Added an answer on June 12, 2026 at 9:14 am

    In Java, the ”protected” access qualifier allows access not only by subclasses, but by other classes in the same package.

    If your last code snippet is in the same package as Child1 and Child2, then it would have access to the protected methods in Child1 and Child2.

    EDIT:

    You can fix this problem and keep your polymorphism by introducing your own base class, in your own package.

     public MyBaseClass extends BaseClass { 
        @Override
        protected void protectedMethod(OtherObject object) {
            super.protectedMethod(object);
        }
     }
    
     public Child1 extends MyBaseClass { ... }
     public Child2 extends MyBaseClass { ... }
    

    And then work with a collection of your own base class type.

     NyBaseClass[] objects = {
        new Child1(),
        new Child2()
     }
    
     for (MyBaseClass object : objects) {
        object.protectedMethod(other); 
     }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this scenario: One custom class (Customer) with some properties, like this: public
I have a classic Order -> Order Row scenario something like this: public class
I am a NHibernate newbie. Suppose you have a Customer class like this public
Lets say I have a set of model classes like this: public class Person
I have a model that looks something like this: public class SampleModel { public
If I have a class like this: public class Foo { public IEnumerable<Bar> Bars
I have the following scenario: public class Controller { private ModelRepository repository; public int
So here's the scenario. I have a class A, and I want some of
I have some code below. This code is a basic push/pop stack class that
Scenerio: I have some divs populated through foreach loop like below <div id=div1><input type=hidden

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.