title: Inheritance for fields in java
body: <p>I'm having trouble coming up with a better solution to the following problem. I'm supposed to use the visibility of &quot;public Warrior warrior&quot; in my aggressiveWarrior class which extends this parent abstract class. The professor tells me that the visibility being public is the key but I can't seem to figure out how to use it correctly. I need to return the attack level of this warrior where say if I test '10' I end up getting the default '3' that I set in the constructor. I have looked at other inheritance posts but I now seem to be even more confused on why what I have tried isnt working. I'm not allowed to change the Builder to a non static class because the tests wont work.</p>
  <pre><code>public class AggressiveWarrior extends Warrior {
  
      private int level;
      private int attack;
      private int defense;
  
      private AggressiveWarrior(int level) {
          this.level = level;
      }
  
      @Override
      public int getLevel() {
          return this.level;
      }
  
      @Override
      public int getAttack() {
          return this.attack;
      }
  
      @Override
      public int getDefense() {
          return this.defense;
      }
      
      
      public static class Builder extends Warrior.Builder{
  
          private AggressiveWarrior aggressiveWarrior;
          public Builder(int level) {
  
              aggressiveWarrior = new AggressiveWarrior(level);
              aggressiveWarrior.attack = 3;
              aggressiveWarrior.defense = 2;
  
          }
          
          public Warrior.Builder attack(int attack) {
               return this; // have tried return this.warrior.attack; 
          }
          public Warrior.Builder defense(int defense){
              return this;
              
          }
          public Warrior build(){
              return aggressiveWarrior;
          }
                  
          
      }
      
  
  }
  
  public abstract class Warrior {
  
      public int level; 
      public int attack;
      public int defense;
  
  
      public abstract int getLevel();
      public abstract int getAttack();
      public abstract int getDefense();
      
      
      public static abstract class Builder{
          
      public Warrior warrior;
      
      public Builder attack(int attack) {
          warrior.attack = attack;
          return this;
      }
      
      public Builder defense(int defense) {
          warrior.defense = defense; 
          return this; 
      }
      
      public Warrior build() {
          validate(warrior);
          return warrior;
      }
      
      public void validate(Warrior warrior) {
          if (warrior.level &lt;= 0 &amp;&amp; warrior.attack &gt; 0 &amp;&amp; warrior.defense &gt; 0) {
              throw new IllegalStateException(&quot;Level must be greater than 0. &quot;);
  
          } else if (warrior.defense &lt;= 0 &amp;&amp; warrior.attack &gt; 0 &amp;&amp; warrior.level &gt; 0) {
              throw new IllegalStateException(&quot;Defense must be greater than 0. &quot;);
  
          } else if (warrior.attack &lt;= 0 &amp;&amp; warrior.defense &gt; 0 &amp;&amp; warrior.level &gt; 0) {
              throw new IllegalStateException(&quot;Attack must be greater than 0. &quot;);
  
          } else if (warrior.attack &lt;= 0 &amp;&amp; warrior.level &lt;= 0 &amp;&amp; warrior.defense &gt; 0) {
              throw new IllegalStateException(&quot;Level must be greater than 0. Attack must be greater than 0. &quot;);
  
          } else if (warrior.level &lt;= 0 &amp;&amp; warrior.defense &lt;= 0 &amp;&amp; warrior.attack &gt; 0) {
              throw new IllegalStateException(&quot;Level must be greater than 0. Defense must be greater than 0. &quot;);
  
          } else if (warrior.attack &lt;= 0 &amp;&amp; warrior.defense &lt;= 0 &amp;&amp; warrior.level &gt; 0) {
              throw new IllegalStateException(&quot;Attack must be greater than 0. Defense must be greater than 0. &quot;);
          }
  
          else if (warrior.level &lt;= 0 &amp;&amp; warrior.defense &lt;= 0 &amp;&amp; warrior.attack &lt;= 0) {
              throw new IllegalStateException(
                      &quot;Level must be greater than 0. Attack must be greater than 0. Defense must be greater than 0. &quot;);
          }
  
      }
      
      }
  
  }
  </code></pre>