Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Damien

macrumors regular
Original poster
Mar 2, 2004
244
31
Canterbury
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?
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
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.
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
Damien said:
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?
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.
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
plinden said:
The only way to do this in Java is to add a method moveWorld(int distance) to both your abstract class and subclass.
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.
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
jsw said:
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.
i believe what he wants to do is call this->moveWorld(int) from a member function coded in the baseclass.
 

plinden

macrumors 601
Apr 8, 2004
4,029
142
jsw said:
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.
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.
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
zimv20 said:
i believe what he wants to do is call this->moveWorld(int) from a member function coded in the baseclass.
plinden said:
Well, that means you have to use the subclass everywhere, ie. instead of:
...
So either you add the single parameter method to both the abstract and sub classes, or you code to e.g. ignore -1.
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.
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
jsw said:
the derived class could have its moveWorld(int,int) method call its moveWorld(int) method, thus not breaking anything.
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.
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
Damien said:
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?
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.
 

Jedi128

macrumors 6502
Jul 7, 2005
274
0
New York, NY
plinden said:
The only way to do this in Java is to add a method moveWorld(int distance) to both your abstract class and subclass.

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?
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
ChrisBrightwell said:
I didn't write anything to test this, but I think it should work.
technically, yes, that will work. it may break the design though. we don't have enough info.

EDIT: I assume that you're using Java, since you called it a method instead of a function.
fwiw, many programmers were calling C++ member functions "methods" years before java was invented. i wonder if that came from smalltalk.
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
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.
 

Damien

macrumors regular
Original poster
Mar 2, 2004
244
31
Canterbury
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
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
Damien said:
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
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.
 

Damien

macrumors regular
Original poster
Mar 2, 2004
244
31
Canterbury
zimv20 said:
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.

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
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
Damien said:
the object of his exercise is to teach inheritance.
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.
 

Damien

macrumors regular
Original poster
Mar 2, 2004
244
31
Canterbury
zimv20 said:
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.

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
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
ChrisBrightwell said:
Not without implementing two different methods, no.

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);
 

Fender2112

macrumors 65816
Aug 11, 2002
1,141
402
Charlotte, NC
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
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
Fender2112 said:
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

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).
 

Fender2112

macrumors 65816
Aug 11, 2002
1,141
402
Charlotte, NC
kingjr3 said:
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).

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)
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
Fender2112 said:
f you use overloading, is it necessary to use a subclass?
no, not at all.

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.
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.

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.
...while function overloading isn't even an OO concept, imo.
 

GeeYouEye

macrumors 68000
Dec 9, 2001
1,669
10
State of Denial
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.