Results 1 to 2 of 2
  1. #1
    Fedor is offline Member
    Join Date
    Apr 2009
    Posts
    5
    Rep Power
    0

    Default Calling Object.method

    Hi,

    In a class named 'Kid' I have a property called Parent of type Object. The actual class of this object can by any of a collection of container classes. In my example I've made two classes called Mum and Dad.

    Alle possible parent classes have a method called doSomething(). When I try to call this method (Parent.doSomething()) the compiler says it doesn't know this method.

    Here is the example:


    Java Code:
    class Mum
    {
       Kid myKid;
    
       public void doSomething(int What)
       {
          ...
       }
    
       public void MakeChild()
       {
          myKid=new Kid();
          myKid.Parent=this;
       }
    
          ... typicall Mum's stuff
    }
    
    class Dad
    {
       Kid myKid;
    
       public void doSomething(int What)
       {
          ...
       }
    
       public void MakeChild()
       {
          myKid=new Kid();
          myKid.Parent=this;
       }
    
    
       ... typicall Dad's stuff
    }
    
    class Kid
    {
       Object Parent;
       
        public void AskParentToDoSomething()
        {
            Parent.doSomething();
        }
    }
    My question here is: what is going wrong and is there a way to get this done?

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

    Default

    The compiler has no knowledge that Parent can definitely call the doSomething method, since Parent is an Object, the only methods available to it are Object's innate methods.

    One solution (that I don't like) is to cast your Parent variable as a Mum or a Dad.

    I think that a better solution is to create an interface, let's call it Parentable, that has one method declared in it, doSomething, and have Mum and Dad implement this interface. Then declare your Parent variable as a Parentable object.

    One error I see in your code is that both Mum and Dad's doSomething methods take an int parameter, and you are trying to call the method with no parameter. You'd better fix that.

Similar Threads

  1. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 12:58 PM
  2. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 05:10 AM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. Calling a method for all instances of an object
    By rattle in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 02:10 PM
  5. Object from String (calling method dynamically)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:22 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
  •