Today a professor of mine commented that he found it odd that while SWT's philosophy is one of making your own controls by composition, Swing seems to favour inheritance.

I have almost no contact with both frameworks, but from what I remember in C#'s Windows Forms one usually extends controls, just like Swing.

Being that generally people tend to prefer composition over inheritance, why didn't Swing/Windows Forms folks favour composition instead of inheritance?

  • 2
    A lot has changed in the 15-20 years those APIs have been around! Rendering engines didn't have magic XML glue to bind screen objects against instances of any arbitrary concrete class "back in the day" ;)
    – Anonymous
    Commented May 25, 2011 at 21:37
  • 1
    Most Swing code I see uses extension by composition. I'm not sure where your prof is getting his data.
    – Anonymous
    Commented May 25, 2011 at 21:39
  • I myself have seen a lot of swing on the net with inheritance instead of composition -- mostly tutorials though. but windows forms really are used based almost solely by inheritance! Commented May 25, 2011 at 22:02

5 Answers 5


JComponent exposes a lot of functionality. If JComponent was an interface and components were implemented with composition, simple components would need to have dozens of trivial method wrappers, e.g.

class MyComponent implements JComponent {
    JPanel panel;
    public boolean contains(int x, int y) {
        return panel.contains(x, y);

There's also an efficiency reason to prefer inheritance over composition -- overriding costs nothing (assuming no super call), while composition costs an extra INVOKEVIRTUAL. I don't know if this influenced the design of Swing, but it's a big concern for collection classes.


The Swing Framework is actually designed in accordance with the Composite Design Pattern. Granted that there is a lot of inheritance in there, but you would usually compose your own forms using composition. That is, a form is a composition of intermediate level containers and controls.

  • "That is, a form is a composition of intermediate level containers and controls." Sure. But what I see usually is that when people want to create their own window (or whatever that is called in Swing), they'll inherit from a window class instead of using composition. Commented May 25, 2011 at 22:04
  • @devoured elysium That's true. But to create the form they would use composition. So it is a little bit of inheritance and a lot of composition.
    – Anonymous
    Commented May 25, 2011 at 22:08
  • @devoured, I think that's a case of people not realising that the tutorial they use isn't following best practice because it's favouring brevity. Commented May 26, 2011 at 12:37

With Java, its a lot easier to end up using inheritance just because everything is virtual. Need to fix a "feature" in JTable/JFrame? Extend it, override the problem methods, and then use your Table/Frame everywhere instead.

I think with things like WPF, where data binding is a primary feature of the design, makes it a lot easier to do composition instead of inheritance.

  • What do you mean with "everything is virtual"?
    – Jonas
    Commented Jul 12, 2011 at 20:17
  • in java, every method is implicitly virtual (can be overrriden). In C#, you have to explicitly declare a method as virtual, and to override it, you explicitly declare it as an override. In java, you can override anything you can see, and you can increase its visibility in a subclass (you can make protected methods public in a subclass!) Commented Jul 12, 2011 at 20:46
  • Note that you can't override a final method in Java, even if the base class itself is not final.
    – perp
    Commented Jul 13, 2011 at 13:39
  • that's true @perp. but in java you have to go out of your way (adding final) to prevent virtual. C# is the opposite way, you have to go out of your way to be virtual. And a very small percentage of the standard java runtime is marked final. Commented Jul 13, 2011 at 16:24

In Effective Java, Item 17, Bloch mentions that a class designed for inheritance "must document its self-use of overridable methods." A hallmark of this is the phrase this implementation. You'll see it in classes like JTable and JInternalFrame. It's one measure of inheritance by design in Swing.


From C# 3.5, we have a concept called as Extension Methods which allows the concept of composition than inheritance.

In this process, we implement an extended functionality to an existing class just by adding an extension class which renders the new feature to the existing class.

You can refer here for more details

  • I don't see the relevance to creating new WinForms Control classes. Could you elaborate? Commented Jul 1, 2011 at 8:42
  • @Peter: This is not related to only windows forms classes. This can be applicable from our code also. You can extend any of the existing class by just adding a static class and then adding new method with the 1st argument as this so that the base object can be linked to. After you compile the code, you get the newly added method as a method of the base class itself. This is what composition states. hope i am right..
    – Saravanan
    Commented Jul 1, 2011 at 11:54
  • 1
    I know what extension methods are, and they're quite handy at times, but this question is about different approaches to creating new classes. Commented Jul 1, 2011 at 12:28
  • @Peter: Then i can only point to the use of partial classes only, other than that to my understanding C# does not have any other remarkable feature. If you know of any, please do let me know.
    – Saravanan
    Commented Jul 4, 2011 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.