Results 1 to 8 of 8
- 05-16-2009, 05:58 PM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 12
[SOLVED] Disabling Inherited Methods
Hi there all.
Is it possible to disable methods inherited from a superclass? I have a class that extends JTabbedPane, and I want to prevent other classes from seeing the addTab methods, while using them in my own addTab method. Here is the class so far:
Java Code:private class AssociatedJTabbedPane extends JTabbedPane{ private static final long serialVersionUID = 1L; private ArrayList<Inventory> associated = new ArrayList<Inventory>(); //Prevent other classes from seeing the addTab methods from JTabbedPane, but //allow them to use this one. public void addTab(String message, JPanel add, Inventory associated){ addTab(message, add); this.associated.add(associated); } }
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 05-16-2009, 06:16 PM #2
Hi,
I don't think it is possible.If you want to restrict the method functionality you do it in the child class.If java allows this type of manipulations,then you are defeating the purpose of OOps itself.
-Regards
RamyaRamya:cool:
- 05-16-2009, 06:43 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 12
Thanks... guess I just need to leave it and only use the new method.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 05-16-2009, 07:34 PM #4
Can you simply not override addTab and make it call your overloaded addTab passing a null as the Inventory parameter?
- 05-16-2009, 08:28 PM #5
Overriding a method prevents any other classes from calling the old version. You can call it from within your class using super.method(). Some arrangement like:
Java Code:public void addTab(String message, JPanel add, Inventory associated){ super.addTab(message, add); this.associated.add(associated); } public void addTab(String message, JPanel add) { addTab(message, add, new Inventory()); }
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-16-2009, 09:51 PM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 12
hmm... thanks for that OrangeDog. Will have to throw an UnsupportedOperationException to disable it b/c Inventory takes parameters.
@ Mr.Beans That wouldn't work in this case as the null parameter would destroy everything...If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 05-16-2009, 09:59 PM #7
The best thing to do is have some kind of default Inventory or action to take when no Inventory is provided.
UnsupportedOperationException should be used as a very last resort, as most users won't expect to catch it, and compilers can't check for it.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-17-2009, 08:04 AM #8
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 12
Well, it is an open-source project, and the Exception is just for other developers to prevent them from using methods that won't do what they want. Also, an Inventory is a user-created object, so having a default would mess with all the other code.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
Disabling a button at EOF
By dbashby in forum New To JavaReplies: 1Last Post: 03-10-2009, 03:37 PM -
How do I store multiple Strings with an inherited class?
By trojansc82 in forum New To JavaReplies: 0Last Post: 11-15-2008, 11:09 PM -
inherited from the Component class
By ronald christian in forum New To JavaReplies: 1Last Post: 10-24-2008, 08:55 PM -
hiding inherited methods
By java_fun2007 in forum New To JavaReplies: 1Last Post: 01-05-2008, 03:16 PM -
disabling JButtons after win in TicTacToe
By noisepoet in forum New To JavaReplies: 1Last Post: 05-19-2007, 12:01 AM
Bookmarks