1 Reply Latest reply on Sep 22, 2008 10:45 AM by 843785

    Encapsulation between class and subclass - (apparent) logic error

      I have the following class set and proposed code but I think it will create
      a programming loop error .... just looking at the pointers

      Am I correct in assuming it will create an error?
      public abstract class MySuperClass {
         private int myCounter = 7;
         private double mySum;
         public int getMyCounter() {
            return myCounter;
         public double setMySum(double myFormula) {
            mySum = myFormula;
            return mySum;
         public double getMySum() {
            return mySum;
      // ... the rest of the code for the class
      The apparent error originates in the subclass ...
      public class MySubClass1 extends MySuperClass {
         private static final double MY_CONSTANT1 = 1000.00;
         private static final double MY_CONSTANT2 = 50.00;
         private double formula = MY_CONSTANT1 + MY_CONSTANT2 * super.getMyCounter();
         // this is the line (I think) would result in a logic error (further explanation below code)
      After setting the formula of the calculation (this is only one subclass of several which
      have individual formulae) ...

      when calling the getMySum() method in the super class, will the "super." prefix remain
      as part of the code?

      That would mean that MySuperClass.getMySum() would be looking for the value of
      super.getMyCounter() ... which doesn't exist

      I presume I could fix it by inserting the line
      private double myCounter = super.getMyCounter();
      into MySubClass ... I could then alter formula like so
      private double formula = MY_CONSTANT1 + MY_CONSTANT2 * getMyCounter();
      but I'm not sure if that is programmatically correct
        • 1. Re: Encapsulation between class and subclass - (apparent) logic error
          Well, your super.setMySum(formula); line is not contained in a method or a constructor (or an initializer block) so that code won't even compile.

          But let's assume it's in the constructor, no it won't cause an error and you don't even need the "super." part in there.

          Also, if there were missing references, they would be caught at compile time, not at run-time. You're assuming that the code is somehow copy-pasted in a macro fashion that would cause the "super" part to be included in other parts.