Results 1 to 8 of 8
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default [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);
    	}
    }
    Note: This is a nested class.
    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!

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    842
    Rep Power
    6

    Default

    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
    Ramya
    Ramya:cool:

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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!

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Can you simply not override addTab and make it call your overloaded addTab passing a null as the Inventory parameter?

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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());
    }
    You can override the method and throw an UnsupportedOperationException to disable it completely.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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!

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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

  1. Disabling a button at EOF
    By dbashby in forum New To Java
    Replies: 1
    Last Post: 03-10-2009, 03:37 PM
  2. Replies: 0
    Last Post: 11-15-2008, 11:09 PM
  3. inherited from the Component class
    By ronald christian in forum New To Java
    Replies: 1
    Last Post: 10-24-2008, 08:55 PM
  4. hiding inherited methods
    By java_fun2007 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 03:16 PM
  5. disabling JButtons after win in TicTacToe
    By noisepoet in forum New To Java
    Replies: 1
    Last Post: 05-19-2007, 12:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •