Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7542391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:09:15+00:00 2026-05-30T08:09:15+00:00

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

  • 0

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 void StartGame() {}
  public void EndGame() {}
}

public class HockeyGame extends Game {
  // ... overrides here
}

public class BaseballGame extends Game  {
  // ... overrides here
}

public class BasketballGame extends Game  {
  // ... overrides here
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:09:16+00:00Added an answer on May 30, 2026 at 8:09 am

    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
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using the CodeIgniter system for a while now - but it
I would like to deserialise Scala case classes that have been serialised using lift-json.
I have been using form inheritance for a while now but without doing much
I have been using this code with great success to pull out the first
I have been using WPF for a few years now and don't have any
I have been using Weblogic as my company used it. Now I want to
I have been using metaprogramming quite a lot, but sometimes the combination of c
I have been creating Intents using putExtra() for quite a while now and just
I have been reading a book and encountered a statement, that generics allow the
Platform: C# 2.0 Using: Castle.DynamicProxy2 I have been struggling for about a week now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.