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 6681129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:33:19+00:00 2026-05-26T04:33:19+00:00

Assuming I have the following classes in an application: A base class: public abstract

  • 0

Assuming I have the following classes in an application:

A base class:

public abstract class Animal {}

A subclass

public class Dog extends Animal {

public void bark() {
    System.out.println("woof");
}
}

A class from a third party library:

public class ThirdPartyLibrary /* *cough* Hibernate *cough* */{
public List find() {
    return new ArrayList() {
        // fetched from the db in the actual app of course
        {
            add(new Dog());
            add(new Dog());
            add(new Dog());
        }
    };
}
}

And a utility class:

public class Util {

public <E extends Animal> E findUnique(List<E> animals) {
    return animals.isEmpty() ? null : animals.get(0);
}

/**
 * @param args
 */
public static void main(String[] args) {
    Dog d = new Util().findUnique(new ThirdPartyLibrary().find());
    d.bark();
}   
}

The eclipse compiler issues the following warnings:

  • Type safety: The expression of type List needs unchecked conversion to conform to List
  • Type safety: Unchecked invocation findUnique(List) of the generic method findUnique(List) of type
    Util

But the build fails when compiling with Sun’s javac with the error:

 incompatible types
    [javac] found   : Animal
    [javac] required: Dog
    [javac] Dog d = new Util().findUnique(new ThirdPartyLibrary().find());
    [javac]                          ^

Is there something else I can do to make this code compile in javac, apart from an explicit cast to (List<Dog>) before the call to findUnique?

  • 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-26T04:33:19+00:00Added an answer on May 26, 2026 at 4:33 am

    No, there’s nothing else you can do, and you must have something fishy going on in Eclipse, because Eclipse wouldn’t be able to compile that (as Java code), either. Since the list you pass to findUnique() is a raw type, the compiler can’t determine what the E should be in public <E extends Animal> E findUnique(List<E> animals). Therefore E essentially becomes ?, and what you’re trying to do is basically this:

    List<? extends Animal> l = new ArrayList<Animal>();
    Dog dog = l.get(0);
    

    It should be obvious that that can never work. The only way to get close to making this work is to add a method argument that makes E have a distinct value. Something like this would work:

    public <E extends Animal> E findUnique(List animals, Class<E> type) {
        return animals.isEmpty() ? null : (E) animals.get(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming i have the following classes: Class MainClass { private OtherClass1; MainClass() { OtherClass1
Assuming I have the following: [Serializable] public class Foo { public Bar bar {
Assuming I have the following classes in different files: <?php namespace MyNS; class superclass
Assuming I have the following delegate public delegate void ControlInitializer(Control control); Is there a
I have the following two classes: class Menu < ActiveRecord::Base has_many :menu_headers accepts_nested_attributes_for :menu_headers
I have the following Entity Framework POCO classes: public class Customer { public int
Assuming I have the following I wish to unit test: class Foo: IAMethods, IBMethods
I have the following two classes. class Settings { function __CONSTRUCT() { echo Settings
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
Assuming I have the following strings: string str1 = Hello World!; string str2 =

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.