Results 1 to 4 of 4
  1. #1
    fangzhong is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default Ask for help on Java access to protected methods

    Hi,
    I use a java toolkit, and I want to access a class (Morphy) in it.

    There is only one constructor function for class Morphy:
    Morphy(WordNet wn).

    As some methods in this class are protected, and I will call them.
    Here is my code:

    import danbikel.wordnet.WordNet;
    import danbikel.wordnet.Morphy;

    public class wn extends Morphy {

    public void test(){
    wn a=new wn();
    a.morphWord("running","v"); // it is a protected method in Morphy
    }
    }

    It just report an compile error: cannot find symbol: symbol : constructor Morphy().

    Can anyone tell me how to fix the problem? I don't know how to add the constructor in my code.

    Thanks a lot in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Whenever you extend a class, your new class's constructor will always first call the parent class's constructor. This can be an explicit call by having a call to super(...) as the first call in the wn constructor, or if not explicitly done, then the compiler will make an implicit call to the default Morphy constructor which has no parameters. Since this constructor doesn't exist, the compiler is throwing the exception. The fix is to have an explicit call to the Morphy constructor with the proper Wordnet parameter as the first line of your wn constructor. i.e.,
    Java Code:
    public wn(WordNet wordnet)
    { 
      // explicit call to the parent class's constructor with proper parameter
      super(wordnet); 
    
      //... the rest of your wn constructor goes here
    }
    Make sense?

    also, please capitalize the first letter of all classes if you want to follow Java coding conventions.
    Last edited by Fubarable; 02-17-2009 at 12:47 AM.

  3. #3
    fangzhong is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    thank you very much, Fubarable.
    It is done following your guidance.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You're welceome, but in the future, please don't cross-post the same question in multiple fora. It is considered rude.

Similar Threads

  1. Replies: 1
    Last Post: 07-20-2008, 05:42 PM
  2. Replies: 5
    Last Post: 01-25-2008, 12:53 AM
  3. Replies: 1
    Last Post: 08-07-2007, 08:53 AM
  4. help with protected method in vector class
    By katie in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 11:59 PM
  5. Replies: 3
    Last Post: 07-30-2007, 08:11 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
  •