title: Java: using setter of a subclass when creating an object of type Superclass
body: <p>I have a superclass Person and two subclasses Man and Woman</p>
  
  <p>in subclass Man, I have an instance variable age:</p>
  
  <pre><code>public class Man extends Person{
  
     private double age;
  
     public final static double MIN_AGE = 0.0;
     public final static double MAX_AGE = 65.0;
  
    //no argument constructor
    public Man(){
      super();
      initAge();  // This randomly selects age
    }
  
     /* Setters and getters */
     public void setAge (double ageIn) 
     {
        /* Assign a value only if it is within the allowed range */
        if (ageIn&gt;= MIN_AGE &amp;&amp; ageIn&lt;= MAX_AGE )
        {
          this.age = ageIn;
        }
        else if (ageIn&lt; MIN_AGE)
        {
          this.age = MIN_AGE;
        }
        else if (ageIn &gt; MAX_AGE)
        {
          this.age = MAX_AGE;
        }
     }
  
    public double getAge()
    {
      return age;
    }
  } 
  </code></pre>
  
  <p>Now, my task is to create a Man object and test whether the setter works and whether initAge works by showing the value of "age: with getAge.</p>
  
  <p>I also have to initialize the object using the following:</p>
  
  <pre><code>Person p1 = new Man();
  </code></pre>
  
  <p>However, if initialized this way, I do not have access to Man's setters and getters. Is there a way around this other than doing:</p>
  
  <pre><code>Man p1 = new Man();
  </code></pre>
  
  <p>Thanks!</p>