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 3 of 3

Thread: Problem with inheritence? arguments not applicable

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problem with inheritence? arguments not applicable

    Hey there!

    I'm working on a project where I have the following classes and interfaces:

    public interface I_SolutionCarrier
    public class Agent
    public class Actor extends Agent implements I_SolutionCarrier
    public class ProblemRastrigin implements I_Problem

    ProblemRastrigin contains a method that uses a Solution to calculate a value.
    It can get this solution by calling the getSolution() method which is defined in I_SolutionCarrier and implemented by Actor.


    Now I'd like to have a sort function in the class ProblemRastrigin that uses the getSolution() method to sort a LinkedList of actors. In the future however, different types of agents will carry a solution and thus the getSolution() method will be implemented in the Agent class.

    Therefore I'd like the sort method in problemRastrigin to be Sort(LinkedList<I_SolutionCarrier>) instead of sort(LinkedList<Actor>). In principle this works fine, but for this to work I have to cast all elements of a LinkedList<Actor> to get a LinkedList<I_SolutionCarrier> before I can sort.

    Someone told me it was possible to write a method sort(LinkedList<? extends I_SolutionCarrier> list)

    But when I do this, I get errors in the swap method of the sort algorithm (quicksort)
    private  void swap(LinkedList<? extends I_SolutionCarrier> list, int i, int j) {
    		   I_SolutionCarrier tmp = list.get(i);
    		   list.set(i, list.get(j));
    		   list.set(j, tmp); 
    	   }

    Both calls to list.set give the following erros:
    The method set(int, capture#17-of ? extends I_SolutionCarrier) in the type LinkedList<capture#17-of ? extends I_SolutionCarrier> is not applicable for the arguments (int, capture#18-of ? extends I_SolutionCarrier)
    Any ideas on how to get this working?

    Thanks in advance!
    Last edited by rbk; March 29th, 2011 at 07:11 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with inheritence? arguments not applicable

    See Wildcards (The Java™ Tutorials > Learning the Java Language > Generics)
    In the case you posted since you do not need access to the objects themselves (just the list), don't parametize the list
            @SuppressWarnings("unchecked")
    	private void swap(List list, int a , int b ){
    		Object s = list.get(a);
    		list.set(a, list.get(b));
    		list.set(b, s);
    	}

  3. The Following User Says Thank You to copeg For This Useful Post:

    rbk (March 29th, 2011)

  4. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Problem with inheritence? arguments not applicable

    Quote Originally Posted by copeg View Post
    See Wildcards (The Java™ Tutorials > Learning the Java Language > Generics)
    In the case you posted since you do not need access to the objects themselves (just the list), don't parametize the list
            @SuppressWarnings("unchecked")
    	private void swap(List list, int a , int b ){
    		Object s = list.get(a);
    		list.set(a, list.get(b));
    		list.set(b, s);
    	}
    I was new to the wildcard thing. Using the wildcard at all the sort methods except the swap method (where I use the generic type) did the trick.

    Thanks a lot!

Similar Threads

  1. command line arguments
    By rizla in forum Member Introductions
    Replies: 3
    Last Post: December 12th, 2010, 11:14 PM
  2. [SOLVED] generic arguments, binary tree
    By vendetta in forum Collections and Generics
    Replies: 0
    Last Post: February 26th, 2010, 07:40 AM
  3. [SOLVED] How to Put Spaces in the Output of Passed Arguments?
    By EmSaint in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 04:04 PM
  4. Multi-Valued Command Line Arguments
    By joey86 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 29th, 2009, 11:19 AM
  5. How to Pass unlimited Arguments to a Function
    By neo_2010 in forum Java Programming Tutorials
    Replies: 2
    Last Post: July 8th, 2009, 11:39 AM