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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:06:27+00:00 2026-05-15T06:06:27+00:00

Can you cast an object to one that implements an interface? Right now, I’m

  • 0

Can you cast an object to one that implements an interface? Right now, I’m building a GUI, and I don’t want to rewrite the Confirm/Cancel code (A confirmation pop-up) over and over again.

So, what I’m trying to do is write a class that gets passed the class it’s used in and tells the class whether or not the user pressed Confirm or Cancel. The class always implements a certain interface.

Code:

class ConfirmFrame extends JFrame implements ActionListener
{
    JButton confirm = new JButton("Confirm");
    JButton cancel = new JButton("Cancel");
    Object o;

    public ConfirmFrame(Object o)
    {
        // Irrelevant code here
        add(confirm);
        add(cancel);
        this.o = (/*What goes here?*/)o;
    }

    public void actionPerformed( ActionEvent evt)
    {
        o.actionPerformed(evt);
    }
}

I realize that I’m probably over-complicating things, but now that I’ve run across this, I really want to know if you can cast an object to another object that implements a certain interface.

  • 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-15T06:06:28+00:00Added an answer on May 15, 2026 at 6:06 am

    You can cast objects either up or down the type hierarchy; sometimes this is safe, and sometimes it isn’t. If you attempt to cast a variable to an incompatible type (i.e. try to convince the compiler it’s something it’s not), you’ll get a runtime exception (i.e. error). Going to a more general type (like changing an ActionListener into an Object) is called upcasting, and is always safe, assuming the class you’re casting to is one of the ancestors of your current class (and Object is an ancestor of everything in Java). Going to a more specific type (like casting from ActionListener to MySpecialActionListener) only works if your object actually is an instance of the more specific type.

    So, in your case, it sounds like what you’re trying to do is say that ConfirmFrame implements the interface ActionListener. I assume that that interface includes:

    public void actionPerformed( ActionEvent evt);
    

    And then here, in your implementation of that virtual method, you want to delegate the evt to whatever object o was passed into the constructor. The problem here is that Object doesn’t have a method called actionPerformed; only a more specialized class (in this case, an implementation of ActionListener like your ConfirmFrame class) would have it. So what you probably want is for that constructor to take an ActionListener instead of an Object.

    class ConfirmFrame extends JFrame implements ActionListener
    {
        JButton confirm = new JButton("Confirm");
        JButton cancel = new JButton("Cancel");
        ActionListener a;
    
        public ConfirmFrame(ActionListener a)
        {
            // Irrelevant code here
            add(confirm);
            add(cancel);
            this.a = a;
        }
    
        public void actionPerformed( ActionEvent evt)
        {
            a.actionPerformed(evt);
        }
    }
    

    Of course, more explanatory variable names than “o” or “a” would probably help you (and anyone else reading this) understand why you’re passing an ActionListener into another ActionListener.

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

Sidebar

Related Questions

From code how can we know which interfaces one class implements? Example: interface IDrink
I have a QVariant object within a QTreeWidgetItem, how can I cast it to
Everything inherits from object. It's the basis of inheritance. Everything can be implicitly cast
How can I make this java generic cast ? public interface IField { }
EDIT: I found out that I can get it to compile if I cast
I have 2 instances of a class that implements the IEnumerable interface. I would
An interesting question arose today. Let's say I have a .NET object that implements
Can you cast a List<int> to List<string> somehow? I know I could loop through
How can I cast long to HWND (C++ visual studio 8)? Long lWindowHandler; HWND
In Informix, how can I cast a char(8) type into a money type, so

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.