OOP: Problem with ints and calling a method from a abstract class

Discussion in 'Mac Programming' started by Damien, Feb 17, 2006.

  1. macrumors regular

    Joined:
    Mar 2, 2004
    Location:
    Canterbury
    #1
    I have a abstract method that takes in two ints in its header

    abstract void moveWorld (int distance, int speed);

    works fine expect in one sub-class where i have the method but this time it only requires 1 int?

    Is there a way to use a method that was two ints in the abstract class but only 1 in one of the sub classes?
     
  2. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #2
    it would help if you posted some code samples. are you saying you want to put code in the baseclass to call a member function of an object of a derivative type?

    if so, that seems like an odd design to me and i'd look at ways to redesign so that situation doesn't come up. a baseclass shouldn't know about behavior of its derived classes.
     
  3. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #3
    This looks like you're using Java? If so, you can't do that - the subclass method has to exactly match that of the abstract class.

    If I recall correctly, C++ allows you to do this since if you call the method with only one parameter, it will assume you're calling moveWorld(int distance); - having worked in Java for so long, I find this behaviour troubling. It's a shortcut for lazy programmers, although I appreciated when I did work with C++. It's been some time though, so I may be misremembering this.

    The only way to do this in Java is to add a method moveWorld(int distance) to both your abstract class and subclass.
     
  4. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #4
    Maybe my brain is nonfunctional today (or more so than on other days), but couldn't you just add the moveWorld(int distance) method to the subclass - assuming you also had the moveWorld(int distance, int speed) one in there as well? i.e., have both in the subclass, and only the original in the abstract class.
     
  5. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #5
    i believe what he wants to do is call this->moveWorld(int) from a member function coded in the baseclass.
     
  6. macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #6
    Well, that means you have to use the subclass everywhere, ie. instead of:

    Code:
    public int doSomething(AbstractClass myObject) {
        ... do something ...
        return myObject.moveWorld(someParameter);
    }
    you have to specify the concrete class:
    Code:
    public int doSomething(ConcreteClass myObject) {
        ... do something ...
        return myObject.moveWorld(someParameter);
    }
    so you can't reuse your doSomething method.

    So either you add the single parameter method to both the abstract and sub classes, or you code to e.g. ignore -1.
     
  7. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #7
    I see. I assumed that the moveWorld(int,int) was generic but that the moveWorld(int) was a special case, used only by clients of the derived class.

    But, of course, the derived class could have its moveWorld(int,int) method call its moveWorld(int) method, thus not breaking anything.
     
  8. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #8
    it's hard to know, since we don't know the specifics. until we know how the two methods relate to each other, we might as well treat them as foo() and bar() and assume they're unrelated.

    given this:
    Code:
    class Base {
        public:
           void foo(int, int);
    }
    
    class Derived :: public Base {
        public:
            void bar(int);
    }
    this:
    Code:
    void Base::foo(int x, int y) {
        bar(x);
    }
    ...would be a no-no.
     
  9. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #9
    Not without implementing two different methods, no. If you inheret an abstract method, you have to implement it.

    Is the abstracted class fully-abstracted? If not, you could write something in that class that would accept a single int, but in turn call the abstracted method and pass a zero in as the other parameter.

    Example:
    Code:
    abstract class TestInheretance
    {
      // Helper method for single-parameter special case.
      public doSomething(int x)
      {
        this.doSomething(x, 0);
      }
    
      // Implement this method in the child class.
      public abstract void doSomething(int x, int y)
    }
    I didn't write anything to test this, but I think it should work. The idea is that your child class will inheret the helper method and implement the abstracted method.

    Otherwise, you just write that helper method in the child class. :)

    Hope this helps.

    EDIT: I assume that you're using Java, since you called it a method instead of a function. Sorry if this is incorrect.
     
  10. macrumors 6502

    Joined:
    Jul 7, 2005
    Location:
    New York, NY
    #10
    I think this would be the easiest solution to your problem. In Java you can have two methods with the same physical name, but different parameters. This is probably your best bet...... You are using Java right?
     
  11. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #11
    technically, yes, that will work. it may break the design though. we don't have enough info.

    fwiw, many programmers were calling C++ member functions "methods" years before java was invented. i wonder if that came from smalltalk.
     
  12. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #12
    i'm sorry to keep harping on about design considerations, but i've come up with a cheesy "real" example of how the fixes above don't necessarily solve the problem. as always, a circus example illustrates things nicely. consider:

    Code:
    class Performer {
    public:
        virtual void peform() = 0; // derived classes must implement
    }
    
    class Juggler : public Performer {
    public:
        void juggle (int, int); // # balls in left hand to start, # balls in right)
        void perform();
    }
    
    class OneHandedJuggler : public Juggler {
    public:
        void juggle (int); // # balls in hand to start
        void perform();
    }
    
    void Juggler::perform() {
        int balls_left_hand = 0;
        int balls_right_hand = 0;
    
        getBalls(balls_left_hand, balls_right_hand);
    
        juggle(balls_left_hand, balls_right_hand);
    }
    
    void OneHandedJuggler::peform() {
        int balls_in_hand = 0;
    
        getBalls(balls_in_hand);
    
        juggle(balls_in_hand);
    }
    
    a cheesy example, yes, but one which demonstrates that putting juggle(int) in the Juggler baseclass has no place there, as most jugglers have two hands. imo, doing so would break the (cheesy) design.

    in this example, the solution is simple: the derived class OneHandedJuggler handles everything specialized, and we use the generic perform() to kick it off.

    i think what the OP is trying to solve is this: juggle(int) and juggle(int, int) probably have some reusable code. what i would do (in C++, my java is very rusty) is break out the common code into a protected method and keep my design sound.
     
  13. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #13
    I didn't hear them called methods until I got to Java.
     
  14. thread starter macrumors regular

    Joined:
    Mar 2, 2004
    Location:
    Canterbury
    #14
    I am learning Java so I do not get the terms correct all the time but heres the thing.

    I have the abstract method. This is the (int, int) method.

    Now, I have one sub-class which only requires one int. (these a measurements and some require two measurments while some only require one)

    As i understand it there no way to do this, i would have to make a seprate method
     
  15. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #15
    apologies for again going on about design, but...

    this doesn't sound to me like it should be a question of what the language allows, it should be a question of what's good design.

    you say only one derived class needs f(int). it should then be clear that the derived class is where f(int) belongs. why would you even want to put it in the base class?

    it really is that simple.
     
  16. thread starter macrumors regular

    Joined:
    Mar 2, 2004
    Location:
    Canterbury
    #16
    This is what i was thinking but the object of his exercise is to teach inheritance. I now have to wait till monday to find out what amount of freedom i am allowed bbut this is annoying the hell out of me.

    I added a fake unused int to the single int method, and it complied but hits a bug when it trys to run
     
  17. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #17
    inheritance or polymorphism?

    or, if you're talking about having methods f(int) and f(int, int) in the same class, that's neither. it's overloading.
     
  18. thread starter macrumors regular

    Joined:
    Mar 2, 2004
    Location:
    Canterbury
    #18
    overloading eh? I looked at overiding.

    I will see what i can do, but this is basically a` problem i am having in a example invloving inheritance. It may be ok to implement a better solution
     
  19. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #19
    Technically, with Java 5 (Tiger) you can use the newly added varargs support to pass in a variable number of inputs to a method.

    Code:
    abstract void moveWorld(int... args);
    
     
  20. macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
    #20
    If the purpose is to learn about inheritance, why not redefine the function in the subclass as this would override the function of the superclass.

    As quoted from a book I have, "A method defined with the same name as that of the parent class, replaces, or overrides, the inherited definition."

    Then again I'm just a beginner and may be missing the point. :D
     
  21. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #21
    Overriding a method will replace the functionality of a parent class' method with the SAME signature (i.e. same arguments). Overloading, which is needed in this case, will provide different implementations based on the arguments passed in (ie. different input argument signature).
     
  22. macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
    #22
    ah...I knew that ... I was testing you.:D If you use overloading, is it necessary to use a subclass?

    If the intent is to explore inheritance of a subclass, would it be appropriate to define two functions in the parent class, functionA (int, int) and functionB(int), and then call functionB from the subclass. This would explore inheritance as opposed to defining the functionB inside the subclass.

    (if this has already been suggested, pardon my ignorance. It's late. I'm tired. I'm going to bed.) :D

    Looking back at the original post, I see overloading makes more sense if it's within the context of what is being learned from the exercise.
    (now I'm going to bed)
     
  23. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #23
    no, not at all.

    yeah, that's right. i'd go further and say that if functionB were defined in the subclass, thus overriding its definition in the baseclass, then that's an exercise in polymorphism.

    since we haven't seen the actual assignment, we've no idea what the instructor is on about.

    ...while function overloading isn't even an OO concept, imo.
     
  24. macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #24
    I hadn't thought about that, since I've yet to use it, but you're right.

    Thanks.
     
  25. macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #25
    I'd suggest rewriting your ConcreteSubclass moveWorld method to take two arguments. Whether or not you do anything with the second one is irrelevant. Alternately, change your AbstractClass moveWorld method to take one argument, and create another abstract class as a subclass of the first, which has its own moveWorld method which takes 2 arguments, and everything else that needs a moveWorld implementation with 2 arguments can be a subclass of that.
     

Share This Page