Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Java Generics

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Java Generics

    Hi guys,

    Please, this question is from the OCP Java SE 6 Programmer Practice Exams textbook, Assessment Test 2 question 11. Below is the code for the question:


    class A{}
    class B extends A {}
    class C extends B {}

     
      public class Carpet<V extends B>{   //line 1
     
               public <X extends V> Carpet<? extends V> method(Carpet<? super X> e){ //line 2
                  //insert code here
              }
    }
    I don't quite understand the syntax of the generic declaration at the class level(line 1) and at the method level(line 2). Anyone care to explain this to me?

    Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Generics

    What don't you understand about it?

    What happened when you wrote a little test program that used this code?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Java Generics

    The syntax. I don't know if you ever wrote with the C language. There used to be weird syntax like this with the C language.. having to do with pointers. pointers of a pointer, pointer arrays , and so on. I am just trying to understand the syntax. I can't write a code IF I am lost on the syntax. The best option I have concluded is to go find a simple tutorial on Java generics but perhaps, an answer on this will also help me as I meet more complex syntaxes on generics.

    Thanks

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Java Generics

    I hope you do know what the "public class Carpet" stands for.
    The rest is the syntax for generics in java.

    You have a generic type parameter called X and you tell the compiler that, whatever X will later be, it must be a class or interface that extends/implements the class/interface B.

    In your method you use a wildcard type "?" that means you dont care what class is being used here. But it must still extend/implement the class/interface V.

    You will never see any code looking like this. This is constructed purely to mess with people, in reality, only the most twisted of minds would wright anything like it.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Generics

    Well I can tell you that this isn't C and has nothing to do with pointers or arrays.

    I recommend going through the basic tutorials and writing an example program that plays with this code. You keep posting questions from exams, which you won't be able to pass if you can't do this kind of research.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Java Generics

    Ok. Thanks for the flippant response.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Generics

    I'm not trying to be flippant; I'm trying to be helpful. The best way to figure out the type of a questions you've been asking is to visit the basic tutorials, do some googling, and write a little test program. That should be your first step, and telling you to try that out is the best thing I can do to help you.

    If you do the googling, read the tutorials, and write a test program and are still confused, then by all means, post here what you've tried and what you're confused about. If you can't do the bare minimum, then maybe programming isn't for you. But it's up to you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Java Generics

    Quote Originally Posted by KevinWorkman View Post
    I'm not trying to be flippant; I'm trying to be helpful. The best way to figure out the type of a questions you've been asking is to visit the basic tutorials, do some googling, and write a little test program. That should be your first step, and telling you to try that out is the best thing I can do to help you.

    If you do the googling, read the tutorials, and write a test program and are still confused, then by all means, post here what you've tried and what you're confused about. If you can't do the bare minimum, then maybe programming isn't for you. But it's up to you.
    And you think I have not thought through that OR I don't know that I have to test something before asking? I was interested in understanding the complex syntax. IF I had not donw some googling, I won't even know it is generic. I just hope one could be given the benefit of doubt when one posts his question.
    Last edited by help_desk; August 7th, 2014 at 01:20 PM. Reason: format

Similar Threads

  1. [SOLVED] Generics
    By stewbond in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 17th, 2013, 04:31 PM
  2. Need some help with Generics
    By bankston13 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 5th, 2012, 03:06 PM
  3. Generics.
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: December 6th, 2011, 06:53 PM
  4. Generics
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 21
    Last Post: December 6th, 2010, 07:08 PM
  5. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 02:25 AM

Tags for this Thread