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

  • Home
  • SEARCH
  • 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 6336265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:01:22+00:00 2026-05-24T19:01:22+00:00

Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>

  • 0

Consider the following simple code

import java.util.*;

public class MainTest<T extends Object1<?,?>> {
    List<T> list;

    public MainTest(List<T> l) {
        this.list=l;
    }
    public int testCompare() {
        // fails to compile here
        return list.get(0).compareTo(list.get(1));
    }

    public static void main(String[]args) {
        List<Object1Impl> list = new ArrayList<Object1Impl>();
        list.add(new Object1Impl());
        list.add(new Object1Impl());

        MainTest<Object1Impl> test = new MainTest<Object1Impl>(list);
        System.out.println(test.testCompare());
    }
}

interface Object1<E, V> extends Comparable<Object1<E,V>> { }
class Object1Impl implements Object1<Integer, Integer>{
    public int compareTo(Object1<Integer, Integer> o) { return 0; }
}

I am aware that in this case the program will not compile (fails at testCompare() because T is extending unbounded Object1<?,?>). Is there any alternative to fix this besides making MainTest<T extends Object1<E,V>,E,V>?

EDIT: the error message is

The method compareTo(Object1<capture#1-of ?,capture#2-of ?>) in the type Comparable<Object1<capture#1-of ?,capture#2-of ?>> is not applicable for the arguments (T)

I have read Effective Java book but still can’t really think of a solution..

Also, why is it that if I change interface Object1 into an abstract class the program will compile without any problem? This really puzzles me…

EDIT: when I mean changing into abstract class is as follows

abstract class Object1<E, V> implements Comparable<Object1<E,V>>{ 
    public int compareTo(Object1<E,V> o) { return 0; }
}

class Object1Impl extends Object1<Integer, Integer>{ }

this will work (only if using Eclipse, compiling it manually using javac does not work) but I have no idea why

  • 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-24T19:01:24+00:00Added an answer on May 24, 2026 at 7:01 pm

    This is correct; the compiler has no way to verify that list.get(0) and list.get(1) are of the same type; one might be Object1<String, Integer> and the other Object1<BigDecimal, Double>.

    To make sure that they are of the same type, you would have to bind those types:

    public class MainTest<A,B,T extends Object1<A,B>> {
        List<T> list;
    
        public MainTest(List<T> l) {
            this.list=l;
        }
        public int testCompare() {
            // fails to compile here
            return list.get(0).compareTo(list.get(1));
        }
    
        public static void main(String[]args) {
            List<Object1Impl> list = new ArrayList<Object1Impl>();
            list.add(new Object1Impl());
            list.add(new Object1Impl());
    
            MainTest<Integer, Integer, Object1Impl> test = new MainTest<Integer, Integer, Object1Impl>(list);
            System.out.println(test.testCompare());
        }
    }
    

    As far as I know, Java doesn’t allow binding parameter types to classes without specifically specifying them.

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

Sidebar

Related Questions

Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following simple code to create a typesafe equals. This first section allows
Consider the following simple polymorphism ... class Parent { public: someFunc() { /* implementation
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider the following simple code: List <?> list4[] = { Arrays.asList(1,2), Arrays.asList(1,2)}; I do
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following simple code pattern: foreach(Item item in itemList) { if(item.Foo) { DoStuff(item);
Suppose you are given a Class.dll assembly compiled from the following simple code: namespace
Consider the following example code: http://code.google.com/apis/maps/documentation/javascript/examples/streetview-simple.html I can do scrollwheel: false on a mapOptions

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.