title: Java: Casting and Inheritance
body: <p>Okay, so recently I realized that I have some misunderstandings with extending and inheritance inside of java.</p>
  
  <p>So far, my understanding states that you cannot do this</p>
  
  <pre><code>MySubClass myClass = (MySubClass) new MyClass();
  </code></pre>
  
  <p>That makes enough sense to me, but there are a few gabs and work-arounds that I don't know how to succeed in, such as if a person is trying to get a SubClass from a method that returns the SuperClass. For example:</p>
  
  <pre><code>public static MyClass getNewMyClass() {
      return new MyClass();
  }
  </code></pre>
  
  <p>And inside of <code>MySubClass</code> there are specific methods I want to access, such as</p>
  
  <pre><code>public void sayBye() {
      System.out.println("Bye.");
  }
  </code></pre>
  
  <p>So what I would think is I need to do is</p>
  
  <pre><code>((MySubClass) myClass).sayBye();
  </code></pre>
  
  <p>..but of course that throws a <code>ClassCastException</code>.</p>
  
  <p><strong>Summary/Question</strong></p>
  
  <p>So basically my question will be, how do I get the subclass of a returned class from a method, or is it possible?</p>
  
  <p>Example: <code>MyClass.getNewMyClass()</code> returns a new <code>MyClass</code>, but I want to use the method <code>sayBye()</code> in its subclass, <code>MySubClass</code>.</p>
  
  <p><strong>More Information</strong></p>
  
  <p>The reason I'm asking this is because I'm trying to create an API for myself that basically will shorten the development of what I am trying to accomplish. In one of my classes, it contains a whole bunch of methods that returns <code>MyClass</code>. Some method examples could be <code>public MyClass getNewMyClass() {}</code>, <code>public MyClass findMyClass(String name)() {}</code> and so on.</p>
  
  <p>But when I get to exending <code>MyClass</code> for some extra methods, I have about 10 methods from the <code>superclass</code> that returns <code>MyClass</code>, and all of that basically will leave me back to the beginning. Is there the possibility of returning <code>Class&lt;? extends MyClass&gt;</code> instead, or something similar?</p>
  
  <p><strong>Solved</strong></p>
  
  <p>Thank you everyone so much for your help, it is very much appreciated! So I basically decided to go with <code>delegation</code>. To solve my issue, here were all of my code and classes, that allowed me to access the <code>superclass</code>, but return the subclass.</p>
  
  <p>Here was my <code>InheritanceTest.java</code></p>
  
  <pre><code>public class InheritanceTest {
      public static void main(String[] args) {
          MyClass myClass = MySubClass.doMath();
          if (myClass instanceof MySubClass) {
              ((MySubClass) myClass).sayBye();
          }
      }
  }
  </code></pre>
  
  <p>And my <code>MyClass.java</code></p>
  
  <pre><code>public class MyClass {
  
      protected String name;
  
      public MyClass(String name) {
          this.name = name;
      }
  
      public String getName() {
          return name;
      }
  
      public static MyClass getNewMyClass() {
          return new MyClass("Toshimaru");
      }
  
      public void sayHi() {
          System.out.println("Hi.");
      }
  }
  </code></pre>
  
  <p>And finally, where it all seems to be resolved, <code>MySubClass.java</code></p>
  
  <pre><code>public class MySubClass extends MyClass {
  
      public MySubClass(String name) {
          super(name);
      }
  
      public static MySubClass doMath() {
          // Got the new instance of MyClass
          MyClass myClass = getNewMyClass();
          // And turned it into a MySubClass with myClass' properties.
          myClass = new MySubClass(myClass.getName());
          return (MySubClass) myClass;
      }
  
      public void sayBye() {
          System.out.println("Bye.");
      }
  }
  </code></pre>