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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:01:41+00:00 2026-05-21T09:01:41+00:00

Sometimes we have several classes that have some methods with the same signature, but

  • 0

Sometimes we have several classes that have some methods with the same signature, but that don’t correspond to a declared Java interface. For example, both JTextField and JButton (among several others in javax.swing.*) have a method

public void addActionListener(ActionListener l)

Now, suppose I wish to do something with objects that have that method; then, I’d like to have an interface (or perhaps to define it myself), e.g.

  public interface CanAddActionListener {
      public void addActionListener(ActionListener l);
  }

so that I could write:

  public void myMethod(CanAddActionListener aaa, ActionListener li) {
         aaa.addActionListener(li);
         ....

But, sadly, I can’t:

     JButton button;
     ActionListener li;
     ...
     this.myMethod((CanAddActionListener)button,li);

This cast would be illegal. The compiler knows that JButton is not a CanAddActionListener, because the class has not declared to implement that interface … however it “actually” implements it.

This is sometimes an inconvenience – and Java itself has modified several core classes to implement a new interface made of old methods (String implements CharSequence, for example).

My question is: why this is so? I understand the utility of declaring that a class implements an interface. But anyway, looking at my example, why can’t the compiler deduce that the class JButton “satisfies” the interface declaration (looking inside it) and accept the cast? Is it an issue of compiler efficiency or there are more fundamental problems?

My summary of the answers: This is a case in which Java could have made allowance for some “structural typing” (sort of a duck typing – but checked at compile time). It didn’t. Apart from some (unclear for me) performance and implementations difficulties, there is a much more fundamental concept here: In Java, the declaration of an interface (and in general, of everything) is not meant to be merely structural (to have methods with these signatures) but semantical: the methods are supposed to implement some specific behavior/intent. So, a class which structurally satisfies some interface (i.e., it has the methods with the required signatures) does not necessarily satisfies it semantically (an extreme example: recall the “marker interfaces”, which do not even have methods!). Hence, Java can assert that a class implements an interface because (and only because) this has been explicitly declared. Other languages (Go, Scala) have other philosophies.

  • 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-21T09:01:41+00:00Added an answer on May 21, 2026 at 9:01 am

    Why can’t the compiler deduce that the class JButton “satisfies” the interface declaration (looking inside it) and accept the cast? Is it an issue of compiler efficiency or there are more fundamental problems?

    It is a more fundamental issue.

    The point of an interface is to specify that there is a common API / set of behaviors that a number of classes support. So, when a class is declared as implements SomeInterface, any methods in the class whose signatures match method signatures in the interface are assumed to be methods that provide that behavior.

    By contrast, if the language simply matched methods based on signatures … irrespective of the interfaces … then we’d be liable to get false matches, when two methods with the same signature actually mean / do something semantically unrelated.

    (The name for the latter approach is “duck typing” … and Java doesn’t support it.)


    The Wikipedia page on type systems says that duck typing is neither “nominative typing” or “structural typing”. By contrast, Pierce doesn’t even mention “duck typing”, but he defines nominative (or “nominal” as he calls it) typing and structural typing as follows:

    “Type systems like Java’s, in which names [of types] are significant and subtyping is explicitly declared, are called nominal. Type systems like most of the ones in this book in which names are inessential and subtyping is defined directly on the structure of the types, are called structural.”

    So by Pierce’s definition, duck typing is a form of structural typing, albeit one that is typically implemented using runtime checks. (Pierce’s definitions are independent of compile-time versus runtime-checking.)

    Reference:

    • “Types and Programming Languages” – Benjamin C Pierce, MIT Press, 2002, ISBN 0-26216209-1.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application, I sometimes have to create elements dynamically, so I create them
I have tens of files that are 256MB and total in 40GB - These
I have a Php application running on my Server A that consumes a .NET
I have an ObservableCollection of class Customers that I fill from a database query.
I have a function that adds a lot of numbers with decimals.... FLOAT numbers
I know that normally there are no problems using Git from different OSs to
I have written a piece of code in python, in which I am asking
I have a div 30px high and 500px wide. This div can contain two
I am new with Android programming and I am investigation the various approaches to
When I click on a row in the use of an object, in leaks

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.