Solved

JDk1.5.0 Class<T> problem??

Posted on 2006-10-19
5
181 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

828 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