title: Bound mismatch error and java generic method
body: <p>I am getting the following error:</p>
  
  <pre><code>Bound mismatch: The generic method constructPage(WebDriver, int, Class&lt;T&gt;) of type     
  Page&lt;T&gt; is not applicable for the arguments (WebDriver, int, Class&lt;HomePage&gt;). The 
  inferred type HomePage is not a valid substitute for the bounded parameter &lt;T extends 
  Page&lt;T&gt;&gt;
  </code></pre>
  
  <p>I am trying to do a login and return a HomePage, if successful and a LoginPage if not using generics.</p>
  
  <p>I have a base class, Page which is extended by SecuredPage for pages behind the login wall. I wrote a generic helper method that would construct pages of any type. This method is used by the login method on the LoginPage. LoginPage extends Page and HomePage extends SecuredPage. SecuredPage extends Page. The login method works if the LoginPage is returned, but I get the above error trying to return a HomePage. Since HomePage is a subclass of page because its parent class extends Page, I am confused as to why HomePage is not a valid substitute for the bounded parameter <code>&lt;T extends Page&lt;T&gt;&gt;</code>. </p>
  
  <pre><code>public abstract class Page&lt;T extends Page&lt;T&gt;&gt; extends SlowLoadableComponent&lt;T&gt; {
  
      protected static final &lt;T extends Page&lt;T&gt;&gt; T constructPage(WebDriver driver, 
      int timeoutInSeconds, java.lang.Class&lt;T&gt; pageClass) 
      {
          Page&lt;T&gt; p = null;
  
          try {
              Constructor&lt;T&gt; pageConstructor = pageClass.getConstructor(
              WebDriver.class, String.class, Integer.TYPE);
              p = pageConstructor.newInstance(driver, driver.getCurrentUrl(), 
                  timeoutInSeconds);
              p.get();
  
          } catch(Exception e) {
  
          }
  
          return pageClass.cast(p);       
      }
  }
  </code></pre>
  
  <p>This is the SecuredPage class:</p>
  
  <pre><code>public class SecuredPage extends Page&lt;SecuredPage&gt; {
  
      .....
  }
  </code></pre>
  
  <p>And this is HomePage:</p>
  
  <pre><code>public final class HomePage extends SecuredPage {
      ......
  }
  </code></pre>
  
  <p>This is LoginPage:</p>
  
  <pre><code>public final class LoginPage extends Page&lt;LoginPage&gt;  {
  
  
      public final HomePage loginWithGoodCredentials(final User user) {
          return login(user, HomePage.class);
      }
  
      public final LoginPage loginWithBadCredentials(final User user) {
          return login(user, LoginPage.class);
      }
  
  
      public final &lt;T extends Page&lt;T&gt;&gt; T login(final User user, final Class&lt;T&gt;     
              expectedPage) {
          enterUsername(user.getUsername());
          enterPassword(user.getPassword());
           loginButton.click();
  
          return Page.constructPage(getDriver(), getTimeoutInSeconds(), 
          expectedPage);
      }
  }
  </code></pre>