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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:13:26+00:00 2026-06-18T03:13:26+00:00

I’m developing a web-app in Java language, which is composed by a system and

  • 0

I’m developing a web-app in Java language, which is composed by a system and some modules. All of them implement the IAppIdentifier interface and I have all the module references and the system itself stored in a List into the system.

The idea is to design that in such way that every module will be able to access the system itself or another modules if they have the required interface (extended from IAppIdentifier), so they have to ask the system for them.

I have this code which works:

@Override
public IAppIdentifier moduleByClass(Class<? extends IAppIdentifier> clazz) {
    List<IAppIdentifier> iApps = this.get_Iapps();
    for (IAppIdentifier iApp : iApps) {
        if (clazz.isAssignableFrom(iApp.getClass())) {
            return iApp;
        }
    }
    return null;
}

Basically it’s checking that each class from the array is assignable from the required interface and if it is it will return that instance. However the matter is that I have to cast it when it’s returned by the method.

For example I have to implement something like that to obtain system’s instance:

((ISystem) this.get_Service().moduleByClass(ISystem.class))

My question is, is there any way in java to avoid doing that casting again, ergo, to ensure it will return the same type I’m passing as argument at compile time?

  • 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-06-18T03:13:27+00:00Added an answer on June 18, 2026 at 3:13 am

    Change method signature to this one :

    public <T extends IAppIdenfitier> T moduleByClass(Class<T> clazz)
    

    This should work.

    Even if your interface isn’t generic you can still use generics in methods for they own purpose. By this code you provide generic rule that T has to be IAppIdentifier itself or has to extend it. Your method now will return object of type T and take as param class as Class<T>.

    Then in your code whenever you invoke method moduleByClass you don’t have to cast it, for example:

    ISystem = this.get_Service().moduleByClass(ISystem.class);
    

    Cast won’t be needed here and everything will compile.

    There is more info needed according to @XtremeBiker good comment. Inside moduleByClass method it’s needed to cast resulting type to T. So it was:

    return iApp;
    

    But now it should be:

    return clazz.cast(iApp);
    

    Anyway it’s still less annoying to make cast in on place inside method body than doing it everytime when that method is invoke.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am writing an app for my school newspaper, which is run completely online
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.