Statement; import java. ArrayList; import java. Level; import java. Logger; import javax. JFrame; import javax. JPanel; import javax. JScrollPane; import javax. JTable; import javax. DefaultTableModel; import java. Dimension; import java. MouseAdapter; import java.
MouseEvent; import java. Asked By: cosmohorst. Answered By: cosmohorst. A class that exposes different methods depending on whether an optional module is loaded. I wouldn't worry too much about real-world best practices just yet.
That being said, in the real world we favor composition over inheritance. Your rule of "one should only extend a class if an IS-A relation is present" is incomplete. It should end with " Your examples do not fit that criteria. You don't have to extend the JButton class just to set its text. You don't have to extend the JFrame class just to add components to it. You can do these things just fine using their default implementations, so adding inheritance is just adding unnecessary complications.
If I see a class that extends another class, I'm going to wonder what that class is changing. If you aren't changing anything, don't make me look through the class at all.
Back to your question: when should you extend a Java class? When you have a really, really, really good reason to extend a class. Here's a specific example: one way to do custom painting for a game or animation, or just for a custom component is by extending the JPanel class.
More info on that here. You extend the JPanel class because you need to override the paintComponent function. You're actually changing the behavior of the class by doing this. You can't do custom painting with the default JPanel implementation. But like I said, your teacher is probably just using these examples as an excuse to force you to practice extending classes.
Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. When should I extend a Java Swing class? Ask Question. Asked 5 years, 6 months ago.
Active 5 years, 6 months ago. Viewed 8k times. He has recommended that for a JSwing application we are building in class One should extend all JSwing classes JFrame , JButton , JTextBox ,etc into separate custom classes and specify GUI related customisation in them like the component size, component label, etc So far so good, but he further goes on to advise that every JButton should have its own custom extended class even though the only distinguishing factor is their label.
So is this standard practice? Btw, the course where this was discussed is called Best Programming Practices in Java [Reply from the Prof] So I discussed the problem with the professor and raised all the points mentioned in the answers.
Improve this question. Paras Paras 4 4 silver badges 9 9 bronze badges. Why extend JButton and call public methods in its constructor, when you can simply create a JButton and call those same public methods outside of the class? This is quite far from being best practices.
Code duplication like you illustrated is a very bad smell. Just ask him why, don't hesitate to communicate his motivation here. I want to downvote because that's awful code, but I also want to upvote because it's great that you're questioning instead of blindly accepting what a bad professor says.
Alex I've updated the question with the prof's justification — Paras. Show 3 more comments. Active Oldest Votes. This is a classic misuse of inheritance for code reuse. Use a helper method instead. Improve this answer. The OkayButton may claim not to have any extra invariants, it may merely consider the text as a default and intend to fully support modifications to the text. Still not a good idea, but not for that reason.
It doesn't violate it because you can. The principle does not mean that it has to have exactly the same functionality, as this would make inheritance pretty much useless.
Sebb but you can't use it with a function setText button, "x" because that violates the assumed invariant. It's true that this particular OkayButton might not have any interesting invariant so I guess I picked a bad example. But it might.
Philip Dush. Optional 'thank-you' note:. So I've created a new class and called it gamePanel. It extends JPanel. I've added this panel to a frame, along with some buttons, in my main method. Essentially, by pressing buttons, it takes the focus off the panel which doesn't allow my key listeners which were added to the panel to work.
Because gamePanel is a custom panel, and not a normal JPanel, using the method. However, it gives an error at the point in which my gamePanel object calls the method. So my ultimate question is, how can I get this working, or how can i return focus to my object?
0コメント