Results 1 to 6 of 6
  1. #1
    Whatty is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default Casting to an List<Serializable>

    Good afternoon,

    I am trying to return a List<Serializable> from a method

    Java Code:
        public List<Serializable> getDBInfo() {
            return ApplicationConfiguration.getInstance().getDbDescriptors();
        }
    where the method getDbDescriptors is:

    Java Code:
        public List<DBDescriptor> getDbDescriptors() {
            return this.dbDescriptors;
        }
    but it is complaining about

    Java Code:
    Multiple markers at this line
    	- Type mismatch: cannot convert from List<DBDescriptor> to 
    	 List<Serializable>
    	- Cannot cast from List<DBDescriptor> to List<Serializable>
    so I figured I would just cast it to the type expected:

    Java Code:
            return (List<Serializable>)(ApplicationConfiguration.getInstance().getDbDescriptors());
    but it does not like that either.

    Am I missing something obvious here or is this not possible.

    I could of course just iterate and create a new list but really don't want to create a new object unless I absolutely have too.

    Thanks in advance.

    Whatty

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm no pro at generics, far from it, but what about doing,...
    Java Code:
        public List<? extends Serializable> getDBInfo() {
            return ApplicationConfiguration.getInstance().getDbDescriptors();
        }

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The error message is very explicit. Those two are not the same. Why did you expect it to work?

  4. #4
    Whatty is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    I just read the following whitepaper on Generics and wow!

    Generics Tutorial (Generics in the Java Programming Language) - http: //java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

    However, your suggestion worked, as example:

    Java Code:
        public List<? extends Serializable> getSerializedDBInfo() {
            return ApplicationConfiguration.getInstance().getDbDescriptors();
        }
        
        @SuppressWarnings("unchecked")
        public void serializedDBTest() {
            List<Serializable> results = (List<Serializable>)this.getSerializedDBInfo();
            for (Serializable serial : results) {
                // do something
            }
        }

    to R035198x - yes, you are correct, it is very explicit (thanks I was unaware of that) but already tried to cast it.

    Help instead of flaming someone is a much better approach but that IMHO.

    Whatty

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    I wonder what gave you the idea that I was trying to flame.
    You never mentioned that DBDescriptor is in fact Serializable so it seemed strange to me that you would want to substitute one for the other.
    The good thing is that at least you read the generics tutorial. It's usually pointless to try to use a feature without first reading a relevant tutorial about that feature.

    P.S ignoreList++;

  6. #6
    Whatty is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Sorry I assumed that it was obvious that DBDescriptor was in fact Serializable, and the reason I mentioned the quote-unquote flame; was that your post did not offer any real help.

    Thanks for the assistance nonetheless

Similar Threads

  1. Casting
    By zzpprk in forum Advanced Java
    Replies: 13
    Last Post: 08-13-2009, 08:59 PM
  2. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 12:07 AM
  3. Replies: 0
    Last Post: 12-31-2008, 03:20 PM
  4. Implementing Serializable interface
    By javaplus in forum Advanced Java
    Replies: 4
    Last Post: 12-18-2007, 01:29 PM
  5. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •