Solved

JDk1.5.0 Class<T> problem??

Posted on 2006-10-19
5
180 Views
Last Modified: 2010-03-31
I have a small program:
===============

public class TestMe
{
        public TestMe() {}
        public static void print (Class<TestMe> cls)
        {
        }
        public static void main (String [] args)
        {
                print(ABC.class);
        }
}

class ABC extends TestMe
{
        public ABC() {}
}

==========================
It fails to compile due to the following reason:

TestMe.java:12: print(java.lang.Class<TestMe>) in TestMe cannot be applied to (java.lang.Class<ABC>)
                print(ABC.class);

Any Idea of solving this little problem?????
0
Comment
Question by:zollen
  • 2
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
shinobun earned 50 total points
ID: 17767212
Try this:

public static void print (Class<? extends TestMe> cls)
0
 
LVL 9

Expert Comment

by:shinobun
ID: 17767216
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17767409
Or leave the print method signature unchanged
But cast the class like this
print((Class<TestMe>) TestMe.class);
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17767428
NO NO You want ABC, so my method is not correct
0
 
LVL 5

Expert Comment

by:kannan_ekanath
ID: 17772772
To generalise, if a Banana extends Fruit. Then a Baseket of Banana does not extend Basket of Fruit. To understand this in an agile/easier way, refer these links,

http://www.agiledeveloper.com/articles/GenericsInJavaPartI.pdf
http://www.agiledeveloper.com/articles/GenericsInJavaPartII.pdf
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question