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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:55:22+00:00 2026-06-10T04:55:22+00:00

I am having a problem with the Visitor pattern and generics. I have some

  • 0

I am having a problem with the Visitor pattern and generics. I have some abstract class whose children are to be visited. Look at this code:

public abstract class Element extends SomeSuperClass {
    public void accept(Visitor<? extends Element> v) {
        v.visit(this);
    }
}

public interface Visitor<T extends SomeSuperClass> {
    void visit(T element);
}

So the idea is: I have some class hierarchy (e.g. Element is a subclass of SomeSuperClass).
I have got some generic Visitor interface to visit this hierarchy. Now in the middle of this hierarchy is the Element class, which is abstract and has it’s own subclasses.

Now I want Element to accept all visitors of its subclasses, which is why I put this line:

public void accept(Visitor<? extends Element> v)

But now I am receiving error:

The method visit (capture#1-of ? extends Element) in the type Visitor<capture#1-of ? extends Element> is not applicable for the arguments (Element).

I understand that ? extends Element is not Element. My question is: Can I express my idea in a different way? Or I have just missed the idea of generics in this case?

  • 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-10T04:55:24+00:00Added an answer on June 10, 2026 at 4:55 am

    Note that T in <T extends SomeSuperClass> can be a type completely unrelated to Element and the compiler must ensure for the general case that visit(T t) will work for every possible T.

    The code you have calls Visitor.visit(Element e), but the visitor in question could be Visitor<SubElement>. That wouldn’t make sense.

    I think that the requirement “Element must accept all visitors of its subclasses” doesn’t make sense: the visitor must at least be able to visit Element and all its subclasses. That would be a Visitor<Element>.

    The construct accept(Visitor<? extends Element> v) means that v can be any such Visitor<T> that T extends Element. It does not mean that the visitor itself will be of the type Visitor<? extends Element>. In fact, no such thing even exists in Java. Every Visitor will have a specific type parameter associated with it, not a wildcard.

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

Sidebar

Related Questions

Im having problem in my UIscrollView ,this is what I have done: Whenever a
I'm having problem with datagrid view. I have attached an image with the code
Having problem with this bit of code qith jQuery. it should pick the values
I am having problem to get a numerical value for this expression where I
I am having problem with drawing multiple lines during repaint. The code is as
hello I am having problem related to https:// . I have used FB.getLoginStatus(function(response) function
I am having a problem with my virtual methods in a derived class. Here
I having problem to update the counter (integer value). this is the definitions of
I having problem marshaling an iterator of objects using JAXB User class: @XmlRootElement(name=User) public
I´m having problem using validation on DropDownListFor ... My Model : public class User

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.