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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:49:43+00:00 2026-05-16T15:49:43+00:00

I have two Java class hierarchies that share a common ancestor and implement a

  • 0

I have two Java class hierarchies that share a common ancestor and implement a common interface. I need to pass a pointer to one of these things to a method in another class.

interface I { ... }

class A extends java.awt.Component implements I { ... }
class B extends java.awt.Component implements I { ... }

class D {
  Component c;
  I i;
  void m(? x) {
    c = (Component) x;
    i = (I) x;
  }
}

Is there something I can replace the ‘?‘ with that will allow me pass in either an ‘A‘ or a ‘B‘? If I cast ‘x‘ to a java.awt.Component and store it in ‘c‘ and to an I and store it in ‘i‘, I lose the benefit of strong typing.

Do I need to declare

class D {
  void m(java.awt.Component c, I i) { ... }
}

and call it with ‘m(a, a)‘ or ‘m(b, b)‘ where

A a = new A();
B b = new B();

I cannot create an

abstract class C extends java.awt.Component implements I {...}

and pass that in because neither A nor B is a C.

BTW, can this be done in Scala?

EDIT: The actual problem that I am trying to solve is that I have two classes, one that extends JInternalFrame and another that extends JPanel. Both are abstract and provide some common functionality for widgets displayed in them (JTables where the user can edit rows, delete rows, etc). The code for editing and deleting rows is always the same, regardless of the underlying object types being displayed. I have several methods that allow the user to click a row, select ‘Delete’ from a popup menu, and, after asking for confirmation, deletes the selected row and database object, for example. Sometimes I need a frame subcomponent and at other times a panel subcomponent. I have created a delegate class for the common functionality and an instance variable in each of the abstract classes of that delegate type. The JInternalFrame and JPanel derived classes then just defer the actual implementations to the delegate. The delegate class, in turn, needs a pointer to the “owner” class for callbacks to get the selected table row, etc., and a pointer to the “Component” nature of each parent for the JOptionPane confirmation dialogs.

Using the Java generics approach has worked very well. The delegate class is now defined as a generic class on <T extends Component & DelegateContainer and each of the owner abstract classes implements DelegateContainer (where the callback methods are declared).

If I were going to rewrite this in Scala, I would probably do something with traits instead of creating a delegate class. The traits could “add” the delete functionality to the JInternalFrame derived concrete class, for example.

Thanks for the prompt replies.

  • 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-16T15:49:44+00:00Added an answer on May 16, 2026 at 3:49 pm

    Generics to the rescue !

    public class Test {
        public static void main(String... args){
            new D().m(new A());
            new D().m(new B());
        }
    }
    
    interface I {  }
    
    class A extends java.awt.Component implements I {}
    class B extends java.awt.Component implements I {}
    
    class D {
      Component c;
      I i;
      <T extends java.awt.Component & I> void m(T x) {
        c = x;
        i = x;
      }
    }
    

    It’s not really the best way to do things but in your case it works. You should really split your method in two, one for the I behavior and another for the Component behavior.

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

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found this website with a nice template supporting factory… May 16, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer You should consider using arrays instead, as those dynamic variables… May 16, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer Qt is your best bet. The Qt license states Nokia… May 16, 2026 at 6:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have two Java classes: B, which extends another class A, as follows :
Currently I am writing a program for an introductory Java class. I have two
I have two java classes. One is NewFrame.java where the forms(buttons, textfields) are located.
I have a simple question (working with Java). I have two classes, one represents
I have two java files in a directory, neither of them are in a
I have two Java classes giveMyOb and dataConn declared in the same directory. Both
Okay, hopefully this is quick and easy. I have two separate java projects, 'Library'
I have a class that unmarshals xml from a 3rd party source (I have
In java world (more precisely if you have no multiple inheritance/mixins) the rule of
Most OO languages prefix their interface names with a capital I, why does Java

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.