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 7662849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:54:24+00:00 2026-05-31T13:54:24+00:00

I wasn’t sure how to properly name this question, so if its better suited

  • 0

I wasn’t sure how to properly name this question, so if its better suited to be edited, please do so. My question has to do with using generic types in an interface and then forcing the implementation into a specific type. Something like this:

public interface Test<T> {
    void testMethod(Object<T> obj);
}

And then instead of allowing a generic object in an implementation of this interface, set the type somehow.

public class TestImpl implements Test<TestObject1> {
    @Override
    public void testMethod(TestObject1 obj) {
        //Do something
    }
}

public class Test2Impl implements Test<TestObject2> {
    @Override
    public void testMethod(TestObject2 obj) {
        //Do something
    }
}

Except you cannot paramaterize Object and i’m not sure how to set this sort of thing up. Is it even possible? Right now I just use generic Object, but that leaves me being forced to check the type of class being passed in for every single method and/or casting etc etc. It would be so much cleaner if I could just use generics on the interface and then specific classes on the implementation.

  • 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-31T13:54:25+00:00Added an answer on May 31, 2026 at 1:54 pm
    public interface Test<T> {
        void testMethod(T obj);
    }
    

    You were close.

    Then you can either write your classes the way you have them if testMethod is specific to the Class being passed in, or …

    public class TestImpl<T> implements Test<T> {
        @Override
        public void testMethod(T obj) {
            //Do something
        }
    }
    

    Now you can instantiate your class via new TestImpl<TestObject>() or new TestImpl<TestObject2>()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wasn't sure how to title this question. I create a UDP connection using
I wasn't sure what to title this question. Here's my goal: On page one,
Okay I wasn't really sure how to word this question, but basically what I
I wasn't sure what the best title is so if anyone has a better
I wasn't sure how to word this question, so i'll just explain. I want
Wasn't sure how to write a good title for this question... :) I'm new
I wasn't sure what to put into the title for this...please read on... I
I wasn't sure how to word the question for this topic...sorry. I'm just starting
The title of this Question may not be accurate because I wasn't sure how
I wasn't entirely sure how to name this, so apologies in advance. You see,

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.