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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:58:52+00:00 2026-06-18T07:58:52+00:00

I am with a sort of trouble when using java generics in the visitor

  • 0

I am with a sort of trouble when using java generics in the visitor pattern.

My code is something like that:

public interface MyInterfaceVisitor<A, B> {
    public A visitMyConcreteObject(MyConcreteObject object, B parameter);
}

public interface MyObject {
    public <A, B> A accept(MyInterfaceVisitor<A, B> visitor, B parameter);
}

public class MyConcreteObject implements MyObject {
    @Override
    public <A, B> A accept(MyInterfaceVisitor<A, B> visitor, B parameter) {
        return visitor.visitMyConcreteObject(this, parameter);
    }
}

public class MyConcreteVisitor implements MyInterfaceVisitor<????> {

    @Override
    public <X extends C> X visitMyConcreteObject(MyConcreteObject object, Class<X> parameter) {
        // Do a lot of things.
        // Return an instance of the given class.
    }

    // This method is the entry point of the MyConcreteVisitor.
    public <X extends C> void someOtherMethod(Class<X> parameter) {
        MyObject m = ...;
        X x = m.accept(this, parameter);
        ...;
    }
}

public class C {}
public class Dog extends C {}
public class Cat extends C {}

public class Client {
    public static void main(String... args) {
        MyConcreteVisitor v = new MyConcreteVisitor();
        v.someOtherMethod(Cat.class);
        v.someOtherMethod(Dog.class);
    }
}

// We have other implementations of the visitor that does not matters, like this one.
public class SomeOtherConcreteVisitor implements MyInterfaceVisitor<String, Integer> {
    @Override
    public String visitMyConcreteObject(MyConcreteObject object, Integer parameter) {
        return "foo";
    }
}

I need to find what is the generic signature in the ???? that makes the code compilable allowing the overriden method in MyConcreteVisitor class to match the signature in MyInterfaceVisitor interface.

I can’t change the signature of the visitMyObject in the MyInterfaceVisitor interface, nor its generics. This happens because others implementations of MyInterfaceVisitor exists and their generics have nothing to with the ones from MyConcreteVisitor.

The MyConcreteVisitor class should not have a generic per-se, so the compiler must allow a MyConcreteVisitor v = new MyConcreteVisitor(); without generating the unchecked or rawtypes warning.

If I change the concrete visitMyObject to public C visitMyObject(MyObject object, Class<? extends C> parameter) and declare the ???? as <C, Class<? extends C>>, I would need to add a cast in the someOtherMethod.

How to define the generic type making it compilable without getting the unchecked or rawtypes warning, changing the interface or adding a cast? Is this even possible in java or I am abusing the generics too much?

  • 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-18T07:58:53+00:00Added an answer on June 18, 2026 at 7:58 am

    The issue is that your implementation is trying to introduce another type parameter X extends C to the method visitMyConcreteObject and resolve the B parameter with it. You can’t make visitMyConcreteObject generic with X but try to resolve B with a type parameterized by X, e.g. Class<X>, because B is resolved at the class declaration but X is only declared by a method of the class.

    From what I can see, you have two options. Either make MyConcreteVisitor generic on X:

    public class MyConcreteVisitor<X extends C> implements MyInterfaceVisitor<X, Class<X>> {
    
        @Override
        public X visitMyConcreteObject(MyConcreteObject object, Class<X> parameter) {
            // Do a lot of things.
            // Return an instance of the given class.
        }
    }
    

    Or get rid of X and lose type safety (beyond the concrete type C):

    public class MyConcreteVisitor implements MyInterfaceVisitor<C, Class<? extends C>> {
    
        @Override
        public C visitMyConcreteObject(MyConcreteObject object, Class<? extends C> parameter) {
            // Do a lot of things.
            // Return an instance of the given class.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble to sort Vector for my blackberry app using SimpleSortingVector. My
I'm having trouble using my sort operator since I need to sort only the
I am using the following code to sort an ArrayAdapter alphabetically: adapter.sort(new Comparator<String>() {
I'm using a NSSortDescriptor to sort my tableView but this line is making trouble:
I am having trouble to sort my collection by Collections.sort() using my own Comparator
I'm having trouble using cv::sort functionality in C++ API of OpenCV . I'm trying
I'm having some trouble with a few routines that are using jQuery to dynamically
Getting my head around Android, I've sort of trouble understanding support for multiple devices.
I am just facing trouble to sort out the query I need. I have
I am in trouble to sort the issue of playing the you tube video

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.