0

I will illustrate my question with the below posted code. It was created specifically for this purpose. The fact that it uses inner interface and inner classes is immaterial (it was just a convenience). The code compiles and runs as expected.

In this code I use inheritance with the sole purpose to declare that a class implements an interface, so later on I can cast them up and use polymorphic behavior. Parent classes already have methods to comply with the interface, so no overriding is required.

Swing is used just as an example. Nothing there is conceptually Swing-specific.

import java.util.*;
import javax.swing.*;

public class TextComponent {

    // Borrowed from com.google.gwt.user.client.ui
    interface HasText {
        String getText();
        void setText(String text);
    }

    static class MyLabel extends JLabel implements HasText {

        public MyLabel() {
            super();
        }

        public MyLabel(String txt) {
            super(txt);     
        }
    }

    static class MyTextField extends JTextField implements HasText {

        public MyTextField() {
            super();
        }

        public MyTextField(String txt) {
            super(txt);
        }
    }

    public static void main(String[] args) {
        List<HasText> txtList = new ArrayList<>();
        txtList.add(new MyLabel("Label"));
        txtList.add(new MyTextField("Text Field"));

        for (HasText component : txtList) {
            System.out.println(component.getText());
        }

    }

}

It seems that the proper way to deal with such tasks (declare already implemented interface) is to either use dynamic proxies (as suggested in this SO post) or 3rd-party tools (such as javassist or CGLIB as pointed out here).

QUESTIONS:

  1. So, is there something in-between static inheritance (or composition) and dynamic reflection-based solutions?

  2. Have I missed something really simple?

Community
  • 1
  • 1
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • I've read this three times now. You don't seem to actually ask a question. – Dawood ibn Kareem Mar 03 '14 at 21:35
  • I have 2 questions at the very bottom. – PM 77-1 Mar 03 '14 at 21:58
  • Yes I saw them, but ... I fail to see the problem that you're actually trying to solve. You seek solutions that are "in-between" one thing and another - solutions to _what_, exactly? – Dawood ibn Kareem Mar 03 '14 at 22:04
  • Solutions to enabling upcasting objects of existing classes to an interface that was not declared at compile time but actually is fully implemented by the class. I called it defacto implementation. – PM 77-1 Mar 03 '14 at 22:26
  • Right, I see what you're getting at now, although I had to read your question multiple times. You said your solution compiles, runs, and works as expected. It also seems to me like a good solution. So, are you expecting to find a better way? I can't imagine what that would be. – Dawood ibn Kareem Mar 04 '14 at 01:56
  • I found another way based `java.lang.reflect.Proxy` class. This seems to be quite complicated (though there are 3rd party tools that provide wrappers). I wondered whether it's an *either or* situation. And whether I missed some simple way to achieve the same. – PM 77-1 Mar 04 '14 at 02:03

0 Answers0