Results 1 to 6 of 6

Thread: Multiple generic types in java

  1. #1
    Join Date
    Mar 2010
    Posts
    222

    Multiple generic types in java

    Hello,
    I will try to explain you my problem here. I created a generic class:
    Here is the code for it
    Code:
    Public interface mclass <T,U>
    {
      ...
    }
    With that, I can instantiate objects like this:
    Code:
    mclass <String, boolean> a = new mclass <String,boolean>();
    What I wish I could do it here:
    Code:
    mclass <String> a= new mclass <String>();
    Is there a way to make an optional type with generics? If there is one then please let me know. I am the interested on here.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Multiple generic types in java

    Hello,
    I think there is an error in the statement and you can modify it in this way, so try this line of code in your program
    Code:
    mclass <String, Object> a = new mclass <String,Object>();
    Hope this will help you and if you need more help then you can read the books in the java generics and understand then in detail. I personally do not think that they are difficult to understand.

  3. #3
    Join Date
    Nov 2009
    Posts
    359

    Re: Multiple generic types in java

    Hello,
    I think you can use the following form in your code
    Check out this
    Code:
    mclass <String,Boolean> a = new mclass <String,Boolean>();
    Instead of use the following in your code
    Code:
    mclass <String, boolean> a = new mclass <String,boolean>();
    I hope you are getting the differences between the two.

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Multiple generic types in java

    Hello,
    I think you can also use the following line of code in your code
    Have a look at it
    Code:
    mclass m = <String,?> new mclass <String,Object>();
    But I do not know why you want to do this in your code , what do you want to indicate from this. I hope you will get the correct way to code for your program.

  5. #5
    Join Date
    Nov 2009
    Posts
    335

    Re: Multiple generic types in java

    Hello,
    Declare no type set, and then all will be considered objective:
    Code:
    mclass x = new MaImpl();
    Implement either explicitly by specifying a parametrized types:
    Code:
    Public class mclass1 <X> implements mclass <X, Boolean>{
     :
     :
     :
    }
    
    mclass x = <Long, Boolean> new mclass1 <Boolean>();
    or
    mclass x = new mclass1 <Boolean>();

  6. #6
    Join Date
    Nov 2009
    Posts
    330

    Re: Multiple generic types in java

    Hello,
    From the above post I have some conclusions. In all cases, the declaration of mclass (parametrized interface):

    - Either you declare without any set type
    - Either you declare with all parametrized types

    Only with the implementation that you can reduce the number of parametrized types.

Similar Threads

  1. Multiple suit types in Dead space 2
    By afidelino in forum Video Games
    Replies: 3
    Last Post: 28-01-2011, 07:27 AM
  2. Method with multiple return types
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 05:10 AM
  3. Don't know about Generic Types in C#
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 12:23 PM
  4. JAVA incomaptible types
    By Daren in forum Software Development
    Replies: 1
    Last Post: 05-10-2009, 02:27 AM
  5. Rename multiple file types
    By Preetish in forum Software Development
    Replies: 4
    Last Post: 02-04-2009, 03:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,006,485.25922 seconds with 17 queries