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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:04:46+00:00 2026-05-30T18:04:46+00:00

Say there’s the following base class: package bg.svetlin.ui.controls; public abstract class Control { protected

  • 0

Say there’s the following base class:

package bg.svetlin.ui.controls;

public abstract class Control {
    protected int getHeight() {
        //..
    }
    //...
}

Also, in the same package, there’s a class that inherits:

package bg.svetlin.ui.controls;

public abstract class LayoutControl extends Control {
    public abstract void addControl(Control control);
    //...
}

Then, there’s a third class in another package:

package bg.svetlin.ui.controls.screen;

public abstract class Screen extends LayoutControl {
    //...
}

And, finally, there’s the implementation class, again in a different package:

package bg.svetlin.ui.controls.screen.list;    

public class List extends Screen {

    private final Vector controls = new Vector();

    public void addControl(Control control) {
        height += control.getHeight();
        controls.addElement(control);
    }
}

Even though List inherits from Control, and the getHeight() is protected, there’s the following error:

getHeight() has protected access in bg.svetlin.ui.controls.Control

I’ve checked that my imports are right. I’m using NetBeans.

Any idea what’s wrong? I thought protected fields and methods are visible to the children even if the latter are in a different package.

Thanks!

  • 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-30T18:04:48+00:00Added an answer on May 30, 2026 at 6:04 pm

    I thought protected fields and methods are
    visible to the children even if the latter are in a different package.

    That’s correct. The class itself has an access to the inherited protected members. But, what you’re trying to do it to call the getHeight method on some Control reference. You’re allowed to call it only on this instance!

    For a better understanding, let me quote Kathy Sierra’s SCJP Preparation Guide:

    But what does it mean for a subclass-outside-the-package to have
    access to a superclass (parent) member? It means the subclass inherits
    the member. It does not, however, mean the
    subclass-outside-the-package can access the member using a reference
    to an instance of the superclass. In other words, protected =
    inheritance. The subclass can see the protected member
    only through inheritance
    .

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

Sidebar

Related Questions

say there is a class package { public MyClass { var myA:Number = 10
Say there is: class A(B): ... where B could be object and ... is
Say there is a method Operation , public ObjectOut Operation(ObjectIn input) What is the
Say there was a method in the library public static <E> void doSmth(Collection<E> foo,
Say there are two methods in my library: void com.somepackage.SomeClass.someSink(String s) and int com.someotherpackage.SomeOtherClass.someSource(int
Say there is a class: class x(obj): y = 1 What is faster (or
Say there is a nested class within an outer class class; Can that nested
Say there is an object A A has a strong reference to B B
Say there is a folder, '/home/user/temp/a40bd22344'. The name is completely random and changes in
Say there are multiple requests in a integration test, some of them are local

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.