Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
1  
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 May 25 '11 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 May 25 '11 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! –  devoured elysium May 25 '11 at 22:02
add comment

migrated from stackoverflow.com May 25 '11 at 23:50

This question came from our site for professional and enthusiast programmers.

5 Answers

JComponent exposes a lot of functionality. If JComponent was just 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 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.

share|improve this answer
add comment

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.

share|improve this answer
    
What do you mean with "everything is virtual"? –  Jonas Jul 12 '11 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!) –  John Gardner Jul 12 '11 at 20:46
    
Note that you can't override a final method in Java, even if the base class itself is not final. –  perp Jul 13 '11 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. –  John Gardner Jul 13 '11 at 16:24
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
"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. –  devoured elysium May 25 '11 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 May 25 '11 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. –  Peter Taylor May 26 '11 at 12:37
add comment

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

share|improve this answer
    
I don't see the relevance to creating new WinForms Control classes. Could you elaborate? –  Peter Taylor Jul 1 '11 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 Jul 1 '11 at 11:54
    
I know what extension methods are, and they're quite handy at times, but this question is about different approaches to creating new classes. –  Peter Taylor Jul 1 '11 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 Jul 4 '11 at 3:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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