Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Can not find subclass method

  1. #1
    samanesh is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Can not find subclass method

    This code gives a compile error (Cannot find symbol method k() at sub.k())
    and i need to know why this is. it does not give an error if MainClass has the exact same function k() (return type and arguments)
    but i dont want that. is there any way to make this work?

    Java Code:
    public class MainClass {	
    	public static void main(String[] args){
    		MainClass sub = new SubClass();
    		sub.k();
    	}
    }
    
    class SubClass extends MainClass {
    	void k(){
    		System.out.println("1,2,3, test, test");
    	}
    }

  2. #2
    Squiffy is offline Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    5
    Rep Power
    0

    Default Re: Can not find subclass method

    The trouble is, once you've used a MainClass reference to refer to your SubClass, you're stuck with only being able to call methods that exist in that particular form in MainClass.

    You have two choices:

    1. Use a reference of type SubClass (this makes sense!)

    public class MainClass {
    public static void main(String[] args){
    SubClass sub = new SubClass();
    sub.k();
    }
    }

    class SubClass extends MainClass {
    void k(){
    System.out.println("1,2,3, test, test");
    }
    }



    2. Downcast the MainClass variable to SubClass -- since it does, after all, refer to an object of type SubClass. But this is a bit crazy.

    public class MainClass {
    public static void main(String[] args){
    MainClass sub = new SubClass();
    ((SubClass)sub).k();
    }
    }

    class SubClass extends MainClass {
    void k(){
    System.out.println("1,2,3, test, test");
    }
    }

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

    Default Re: Can not find subclass method

    Better option: simply give MainClass a public void k() method. Why is this not a viable option if you want to treat MainClass variables as if they have this method?

  4. #4
    samanesh is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Can not find subclass method

    for my program the easiest to do will be to add an empty method, I knew this worked but i wanted to know if i could avoid it. thank you for the help.

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

    Default Re: Can not find subclass method

    Quote Originally Posted by samanesh View Post
    for my program the easiest to do will be to add an empty method, I knew this worked but i wanted to know if i could avoid it. thank you for the help.
    If you are going to exepct MainClass variables to call a method, then the method needs to be accessible to the MainClass. Consider making MainClass abstract and the k method just a method definition.
    Junky likes this.

Similar Threads

  1. HELP! can't find symbol-method
    By Jack9333 in forum New To Java
    Replies: 2
    Last Post: 03-04-2011, 02:48 AM
  2. subclass calling superclass method
    By stackptr89 in forum New To Java
    Replies: 10
    Last Post: 11-22-2010, 08:40 PM
  3. Using superclass fields in subclass method
    By lonegreyride in forum New To Java
    Replies: 12
    Last Post: 11-17-2010, 02:21 PM
  4. Subclass Overridden Method throws Exception Rule?
    By hemanthjava in forum New To Java
    Replies: 5
    Last Post: 05-14-2010, 03:20 PM
  5. Can't find method
    By 01allenh in forum New To Java
    Replies: 1
    Last Post: 03-25-2009, 08:46 PM

Posting Permissions

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