Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 316409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:22:54+00:00 2026-05-12T08:22:54+00:00

In my project I have a factory method that loads an object that implements

  • 0

In my project I have a factory method that loads an object that implements an interface. You pass in the class you desire and receive an instantiation of it, like so.

public class Factory {
    public static <E extends SomeInterface> E load( Class<E> clss ) throws Exception {
        return clss.newInstance();
    }
}

You could invoke it like this:

MyObject obj = Factory.load( MyObject.class );

This code works just fine in Eclipse 3.4 with Java 6u13, however today I received a new laptop and installed Eclipse 3.5 and java 6u15 and now I am getting type mismatches everywhere.

MyObject obj = Factory.load( MyObject.class );
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
               Type mismatch: cannot convert from SomeInterface to MyObject

Putting a cast before Factory on that line makes it go away and all runs well, but it makes the line a bit less clean, and I didn’t need it before, so what gives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:22:54+00:00Added an answer on May 12, 2026 at 8:22 am

    Is that all the code required to get this bug? I’ve seen something very similar in some code I’ve been looking at today. There was an additional parameter being passed into the equivalent of your Factory method which had a generic type as well. This was missing it’s generic definition and I think was to blame for confusing the compiler.

    ie, if your factory method looked something like

    public class Factory {
        public static <E extends SomeInterface> E load( Class<E> class, Key key ) {
            // return an instance of E
        }
    }
    

    Where there is some Key class defined something like this

    public class Key<Datatype> {
        ....
    }
    

    Giving something like this to invoke the method, note no generics on the declaration of key

    Key key = new Key()
    MyObject obj = Factory.load( MyObject.class, key );
    

    Hope that helps,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Platform: C# 2.0 WinForms I have a factory class that provides an instantiation of
I have to implement a factory method pattern in C++. The class (C) that
I have a C++ project that have like 15+ external libraries installed with a
I have different fruit class, all implements the same interface IFruit : public interface
I have project that I'm working on that is going to require a webserver.
In my DSL project I have a shape with a number of decorators that
In a recent project I have nearly completed we used an architecture that as
In my windows forms project I have a parent form that is creates other
I have a project that I am working on at my job and I
On a current project which I'm currently working, i have a symfony task that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.