Results 1 to 4 of 4
  1. #1
    Aboogesnickle is offline Member
    Join Date
    Jun 2014
    Posts
    37
    Rep Power
    0

    Default Question about sets

    so I have a class that consists of a LinkedHashSet and methods to add, remove, and clear. The LinkedHashSet is a set of components. The Component class is an interface with an update and dispose method. Now in the class with the LinkedHashSet I have figured out how to access the update and dispose methods using

    Java Code:
    public void update(){
    		for(EntityComponent component: entityComponents){
    			component.update();
    		}
    	}
    and one for dispose.

    So my question is, If I create a specific component that has a method the interface does not have. How do I access it in the set.

    I have played around with it for awhile now and all I have been able to come up with is trying to use the getClass() but I just cant seem to get access to it.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Question about sets

    How do I access it
    cast it to the class that has the method.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Aboogesnickle is offline Member
    Join Date
    Jun 2014
    Posts
    37
    Rep Power
    0

    Default Re: Question about sets

    Quote Originally Posted by Norm View Post
    cast it to the class that has the method.
    Thank you. I did not know you could cast like that.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Question about sets

    I assume you mean you have some class that implements that interface. If you have several different classes that
    implement that interface and have extra methods, then you must cast to the correct class.
    Otherwise you will get a class cast exception.



    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Why not use sets and/or map but Hibernate?
    By claudiocarcaci in forum Hibernate
    Replies: 4
    Last Post: 01-24-2014, 09:10 AM
  2. Question regarding hash sets.
    By Crakrjack in forum New To Java
    Replies: 11
    Last Post: 09-16-2013, 01:48 PM
  3. Sets Question
    By Malv in forum New To Java
    Replies: 3
    Last Post: 07-22-2013, 07:15 PM
  4. Maps and Sets
    By darkblue24 in forum New To Java
    Replies: 19
    Last Post: 03-25-2010, 06:13 PM
  5. Duplicates in more than two sets
    By JavaJ in forum New To Java
    Replies: 8
    Last Post: 12-03-2009, 04:07 PM

Posting Permissions

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