PDA

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




Damien
Feb 17, 2006, 02:11 PM
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
Feb 17, 2006, 02:16 PM
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
Feb 17, 2006, 02:18 PM
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
Feb 17, 2006, 02:25 PM
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
Feb 17, 2006, 02:32 PM
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
Feb 17, 2006, 02:33 PM
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:

public int doSomething(AbstractClass myObject) {
... do something ...
return myObject.moveWorld(someParameter);
}
you have to specify the concrete class:
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
Feb 17, 2006, 02:42 PM
i believe what he wants to do is call this->moveWorld(int) from a member function coded in the baseclass.
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
Feb 17, 2006, 02:49 PM
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:

class Base {
public:
void foo(int, int);
}

class Derived :: public Base {
public:
void bar(int);
}

this:

void Base::foo(int x, int y) {
bar(x);
}
...would be a no-no.

ChrisBrightwell
Feb 17, 2006, 02:52 PM
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: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
Feb 17, 2006, 03:01 PM
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
Feb 17, 2006, 03:04 PM
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
Feb 17, 2006, 03:20 PM
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:


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.

ChrisBrightwell
Feb 17, 2006, 03:24 PM
fwiw, many programmers were calling C++ member functions "methods" years before java was invented. i wonder if that came from smalltalk.I didn't hear them called methods until I got to Java.

Damien
Feb 17, 2006, 04:50 PM
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
Feb 17, 2006, 05:21 PM
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
Feb 17, 2006, 06:16 PM
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
Feb 17, 2006, 06:30 PM
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
Feb 17, 2006, 06:59 PM
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
Feb 17, 2006, 07:34 PM
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.


abstract void moveWorld(int... args);

Fender2112
Feb 17, 2006, 09:46 PM
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
Feb 17, 2006, 10:22 PM
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
Feb 17, 2006, 10:57 PM
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
Feb 17, 2006, 11:15 PM
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.

ChrisBrightwell
Feb 18, 2006, 02:32 AM
Technically, with Java 5 (Tiger) you can use the newly added varargs support to pass in a variable number of inputs to a method.I hadn't thought about that, since I've yet to use it, but you're right.

Thanks.

GeeYouEye
Feb 18, 2006, 02:28 PM
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.

Damien
Feb 19, 2006, 04:57 AM
I got it to work by creating a method that takes one two ints and calls another (the real) method that only takes one.

Cheers people