Thursday, 8 September 2016

Cast Java classes into user defined class


import java.util.LinkedList;
import java.util.List;

public class CastCollection extends LinkedList<String> {

      private static final long serialVersionUID = 1L;

      public static void main(String[] args) {

            class MyList extends CastCollection {
                  private static final long serialVersionUID = 1L;
            };

            MyList list = new MyList();
            list.add("Yasin");

            if (list instanceof CastCollection) {
                  CastCollection castedObj = (CastCollection) list;
                  System.out.println("Casted succesfully");
                  System.out.println(castedObj.get(0));
            }
      }
}
Output:
Casted succesfully
Yasin


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...