Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Cannot find methods in a subclass

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Cannot find methods in a subclass

    Hello, I'm new to programming and am currently exploring the world of OOP.
    However I've come across a problem, I've got a superclass called Object, all of the game objects will extend this Class.
    In the Object class, I've got a few variables and methods wich are working fine.

    So I create a player object:
    Object player = new Player(100, 100); //100, 100 are the x & y variables

    In the Player class I've got an method that is not in the superclass (Object.java).
    When I try to call this method (from another class in the same package) it cannot find the method. It can find all the methods in the superclass, and if I add it to the superclass and then override it in the subclass it works as it should work.
    However I find this very 'clumsy'.

    So to put my question in one sentence:
    "How can I call a method from a subclass that is not in the superclass?"
    I hope that somebody can explain it, or point me in the right direction.

    Thanks in advance,
    Dirk.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,344
    My Mood
    Hungover
    Thanks
    144
    Thanked 625 Times in 536 Posts

    Default Re: Cannot find methods in a subclass

    You almost definitely don't want to call that class Object. Object is already a fundamental class in Java, as every other Object extends it, and overlapping names can lead to much confusion.

    If you want help, please provide an SSCCE that demonstrates the problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Cannot find methods in a subclass

    Quote Originally Posted by Dirk94 View Post
    "How can I call a method from a subclass that is not in the superclass?"
    NO, you can't do that, unless the object you are working on is an instance of the subclass. In that case, you need to cast the object to be type of subclass. For example:

        Super super = new Sub();
        super.invokeAmethodFromSubClass();   // COMPILE ERROR!
        ((Sub) super).invokeAmethodFromSubClass();  // OK

  4. The Following User Says Thank You to Bob_Sadarka For This Useful Post:

    Dirk94 (March 24th, 2012)

Similar Threads

  1. [SOLVED] NullPointerException when using methods to find matching chars in two arrays.
    By Javaisfrustrating in forum Exceptions
    Replies: 4
    Last Post: November 18th, 2011, 03:12 AM
  2. Problem with subclass and superclass methods
    By Farmer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2011, 08:43 PM
  3. [SOLVED] how to extend a subclass?!
    By migongotar in forum Object Oriented Programming
    Replies: 4
    Last Post: August 2nd, 2011, 03:22 PM
  4. subclass that implements
    By speedycerv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2011, 07:35 AM
  5. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM