Skip to main content

How do I call a method of a generic type in a collection which is guaranteed to exist?



I have been using generics for sometime, but only in a simple straightforward fashion.





Now I would like to do this, I have been reading and trying many things, such as using interfaces and wildcards but with no success. I would like to learn if there is a valid way to achieve this with Java generics, or if I have misunderstood they way in which generics should be used.





Lets say I want to make a generic collection class, an ArrayList, and I want to create such arraylists for various differing types BUT IMPORTANTLY differing types which are ASSURED to implement a certain method. I would then like to be able to call that method from within my generic arraylist.





The following code is very simplistic, and clearly will not work, I know that. I have tried plenty more sophisticated ideas than this, however I include the following code just to sum up what I am trying to do, and to provide an example for answers.





See the line of code in DemonstrateProblem()...I would like to be able to call that on the understanding that I will only ever use types in TestContainer which implement the method StartGame()







public class TestContainer<T> extends ArrayList<T> {

public void DemonstrateProblem() {

// assumes we have populated the collection with 5+ objects...

// the line of code below is ultimately what I would like to acheive

this.get(4).StartGame(); // <-- my goal!

}

}



public abstract class Game () {

public abstract void StartGame() {}

public abstract void EndGame() {}

}



public class HockeyGame extends Game {

// ... overrides here

}



public class BaseballGame extends Game {

// ... overrides here

}



public class BasketballGame extends Game {

// ... overrides here

}




Comments

  1. What you want to do is restrict the types that your genericized class will accept to only those that implement the behavior you are expecting. You can do this by:


    Implementing an interface with your expected behavior
    Restricting the bounds of your generic type to that interface.


    Illustrated in an enhanced version of your pseudocode:

    public interface Game {
    public void StartGame();
    }

    public class TestContainer<T extends Game> extends ArrayList<T> {
    public void DemonstrateProblem() {
    // assumes we have populated the collection with 5+ objects...
    // the line of code below is ultimately what I would like to acheive
    this.get(4).StartGame(); // <-- my goal!
    }
    }

    ReplyDelete
  2. You do not need to keep your container generic on T: you can instantiate an existing generic container on Game, like this:

    public class TestContainer extends ArrayList<Game> {
    public void DemonstrateProblem() {
    this.get(4).StartGame(); // <-- should work
    }
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex