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

  • Home
  • SEARCH
  • 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 627051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:29:10+00:00 2026-05-13T19:29:10+00:00

I am using the GWT library. There is a base class called Widget that

  • 0

I am using the GWT library. There is a base class called Widget that all Widgets inherit from. Some Widgets implement certain interfaces (for example HasText), others do not. Sometimes I wish to guarantee that something being passed as an argument to a function is of a certain class AND implements a certain interface.

For example, I wish to have a function that takes a argument X, where X is of the class type Widget AND of the interface type HasText. I wish to have this behavior because only Widgets can be added to Layout containers, and HasText specifies the complete set of behaviors that I actually need from said Widget.


In pseudocode form, it might be:

public void fx(I_AM_A_Widget_AND_IMPLEMENT_INTERFACE_HasText x){
    //do stuff with x, which is guaranteed to be a Widget AND implement HasText
}

Is this in any way possible in Java? If there are multiple ways to do so, is there a preferred/better way?

  • 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-13T19:29:11+00:00Added an answer on May 13, 2026 at 7:29 pm

    Thank you for both of your solutions. In the end, I decided that the best solution was to simply specify bounded types at the top of the class definition. For example, I used the form

    public class Example<Editable extends Widget & HasText> {
      private Editable editor = null;
    
      //do stuff in various methods (including the constructor) with the 
      //editor variable and the Editable type.
    }
    

    I picked this solution for a few reasons.

    1. The two above mentioned solutions both require the use of methods (boilerplate) that do not really relate to business functionality, but are instead related to circumventing the limitations of the language.
    2. Anon, your solution is correct, but I have the problem of being unable to declare a variable that would hold a reference to the generic object your method returns. I believe this to be the case because the generic object returned from your method only exist at the level of the method, and not the class. Perhaps I have missed some syntax that would allow me to declare variables that have generic properties?
    3. Yishai, your solution works as well, but then I have the problem of having to declare a separate method in every interface that I wish to guarantee support for, this seemed cumbersome.
    4. These bounded types declared in the class “header” only need be declared once. It seems like the most terse declaration of generic bounded types available in Java.
    5. You can declare more than one bounded type in the class “header”.
    6. If you use this method exclusively, all the potential bounded types are declared at the same location in your code.

    The declaration of bounded types in the class header is the best solution I have run across. I would still be interested in knowing if their is a way to permanently declare (so that it might be accessed across different classes) a bounded type, rather than declaring them per class.

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

Sidebar

Related Questions

Hello all I am using gwt-ext in my application.In that application session time is
Is there some library for using some sort of cursor over a file? I
The following is when using the drag and drop library from here: http://code.google.com/p/gwt-dnd/ When
I'm using the gwt visualization library to display motion charts in a gwt app.
Im using this library to parse CSV files in my GWT app. However, It
When using GWT I get following warning: Referencing deprecated class 'com.google.gwt.user.client.rpc.SerializableException' While it's only
I need to create javascript library which will be called from other javascript code.
When using GWT 2.0.x and SmartGWT 2.2 Code as simple as: public class SmartGwtTest
I have an xml document (using the Document class in the XMLParser library of
Using GWT with the MVP pattern , I'd like to have a place that

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.