Why is a protected class method not callable from an object created from a subclass?

edited October 2014 in Programming Questions

Hi,

I am using the GI Centre chart library to draw an XY Chart. I have created an object from the XYChart which is a sub-class of the AbstractChart class. I can call various methods from the XYChart class fine in order to modify the object. But I want to call a method from the AbstractChart class (the SetMinBorder method).

I am trying to do it by the below:

    XYChart lineChart;

        void setup() 
        {

          size(800, 600);
          textFont(createFont("Arial", 10), 10);


          // Axis formatting and labels.
          lineChart.showXAxis(true); 
          lineChart.showYAxis(true); 
          lineChart.setMinY(20);
          lineChart.setMinX(0);
          lineChart.setMaxY(70);
          lineChart.setMaxX(current_max_x);
          lineChart.setXAxisLabel("Time (mins)");
          lineChart.setYAxisLabel("Temperature");
          lineChart.setAxisLabelColour(10);
          lineChart.setMinBorder(10);

My issue is that I can't call the setMinBorder method; it returns that the method is "not visible". From a fair bit of internet research, given that the two classes are contained with the same package and that the method is inherited by the XYChart class, I'm not sure why. Any help would be much appreciated.

Thanks.

Answers

  • The method is protected so it is only visible from classes that inherit from this class or classes in the same package. So it is visible to the source code of the XYChart class, but it can not be invoked using an object of type XYChart.

  • edited October 2014

    Members which are protected can be accessed only by subclasses and package! [-X
    Since you've already got a XYChart subclass, hack its parent class into submission! o=>
    Override anything you want w/ your own version and access level: :ar!

    class XYChart extends AbstractChart {
      @ Override void setMinBorder(float border) {
        super.setMinBorder(border);
      }
    }
    
  • edited October 2014

    ... or classes in the same package.

    Only the "default" (and of course public) access level can access members of the same package.
    protected access level only allows subclassing! :-@

  • @GoToLoop

    Members which are protected can be accessed only by subclasses! :-q

    Only the "default" (and of course public) access level can access members of the same package. :-q

    Wrong on both counts - check it out here

  • edited October 2014

    Oops! Somehow in my mind I thought both protected & package were more restrictive than "default" & subclassing! It turns out that protected is mere 1 step away from public. X_X
    I've gotta memorize that table better. I knew about that, but failed to carve it in my brain so far! :O)
    Since "default" doesn't allow inheritance access, it's almost as "efficient" as private! ~:>

  • Easily done :)

    I was still programming in C++ when I first started working with Java and I assumed that the access modifiers behaved in exactly the same. It was some time before I learned the truth about Java protected and that was by accident.

  • edited October 2014

    Dunno C++ but a few of C. Processing doesn't show much opportunity to train Java access.
    And the word protected seems more "serious" than package default access! [-(
    But as said, I've already studied Java access. It's the chosen words for access which led me astray! :-@

Sign In or Register to comment.