title: Cast Set subclass to Set superclass java
body: <p>is there any way to cast <code>Set&lt;Subclass&gt;</code> to <code>Set&lt;Superclass&gt;</code> ?</p>
  <p>I have tried this 3 ways bellow? is there a better way or some best practice for this?</p>
  <pre><code>
  
  // class extends superclass
  final class MapEntryCustom implements Map.Entry&lt;String,Object&gt; {
          // all implementation
  }
  
  // var to be casted
  private Set&lt;MapEntryCustom&gt; entrySet = new HashSet&lt;&gt;();
  
  // 3 trying way of casting
  public Set&lt;Entry&lt;String, Object&gt;&gt; getSetMapEntry() {
     
     return (Set&lt;Entry&lt;String, Object&gt;&gt;) entrySet; // error cannot cast
     
     return (Set&lt;? extends Entry&lt;String, Object&gt;&gt;) entrySet; // error cannot cast
     
     return (Set&lt;Entry&lt;String, Object&gt;&gt;) (Set&lt;? extends Entry&lt;String, Object&gt;&gt;) entrySet; // warning type safe
  
  }
  
  
  </code></pre>