Results 1 to 5 of 5
  1. #1
    slider57 is offline Member
    Join Date
    Aug 2011
    Posts
    29
    Rep Power
    0

    Default Using Generic Methods like Templates?

    I have taking about a year in c++ and am trying to learn java right now. I'm really confused about generics because it looks like templates in c++ to me. Here is a snippet of my code that wont work.

    public static <T> void monsterMain(T monster)
    {
    monster.getStats();
    }

    There are 4 different data types that can be passed to this method. The function in the derived class getStats() works fine, I have tested it other places. The error I get is:
    'cannot find symbol'
    'symbol: method getStats()'

    Each derived class has a redefined function getStats() so the calling data type shouldn't be an issue. Any help would be mucho appreciated.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    What happens if I pass a String to that method? String does not have a getStats method. Perhaps you should be using Inheritance instead of Generics. You say you have 4 different data types. Do they all extend from the same Parent class? If so make the parameter type to your method be the Parent class.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also if they all implement getStats, you can have them all implement and interface and pass in a type of the interface(this is nearly identical to what junky said, except he referred to inheritance)

  4. #4
    slider57 is offline Member
    Join Date
    Aug 2011
    Posts
    29
    Rep Power
    0

    Default

    The four different data types are classes inherited from one parent class. If I make The data type the parent class wouldn't I be guilty of data slicing. Also the function getStats isn't part of the parent class. Would it data slice if I make the argument the parent classes data type?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by slider57 View Post
    The four different data types are classes inherited from one parent class. If I make The data type the parent class wouldn't I be guilty of data slicing.
    Data slicing doesn't exist in Java; it's a artifact in C++ inherited from C. Don't worry about it. b.t.w. you can make your generic type <T extends AbstractMonster> (or whatever) where the AbstractMonster class implements the getState() method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 7
    Last Post: 02-27-2011, 01:17 AM
  2. HELP! - Eclipse Templates
    By protocos in forum Eclipse
    Replies: 1
    Last Post: 03-06-2009, 02:31 AM
  3. Generic methods
    By andre1011 in forum Advanced Java
    Replies: 7
    Last Post: 02-25-2009, 02:17 PM
  4. Writing generic methods
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:28 AM
  5. Templates JSP
    By Eric in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-04-2007, 07:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •