Calling a method of a base class that is redefined in a extendedclass

  • Thread starter robbie.desutter
  • Start date
R

robbie.desutter

Hello,

How can I call a method of a base class from a base method whereby the
called method is redefined in a class that extends the base class

To be more concrete, assume the following classes
Class A {
void methodX() {
do_1;
methodY(); // **
}
void methodY() {
do_2;
}
}

Class B extends A {
void methodX() {
super.methodX();
methodY();
}
void methodY() {
do_3;
}
}

If you create an instance of Class B and call methodX on it, do_1 is
executed and next do_3 two times (once called by MethodX of Class A
and once by MethodX of class B). However the first time (line **) I
want to call methodY of class A, in order to get sequence do_1, do_2,
do_3.

Already tried to replace line ** to
((A)this).methodY();
but this doesn't work as the explicit cast to class A of an instance
of class B results in an instance of class B...

How can I solve this problem, preferably without a slow reflection.

Kind regards,
Robbie De Sutter
 
L

Lionel van den Berg

Hello,

How can I call a method of a base class from a base method whereby the
called method is redefined in a class that extends the base class

To be more concrete, assume the following classes
Class A {
void methodX() {
do_1;
methodY(); // **
}
void methodY() {
do_2;
}
}

Class B extends A {
void methodX() {
super.methodX();
methodY();
}
void methodY() {
do_3;
}
}

If you create an instance of Class B and call methodX on it, do_1 is
executed and next do_3 two times (once called by MethodX of Class A
and once by MethodX of class B). However the first time (line **) I
want to call methodY of class A, in order to get sequence do_1, do_2,
do_3.

Already tried to replace line ** to
((A)this).methodY();
but this doesn't work as the explicit cast to class A of an instance
of class B results in an instance of class B...

How can I solve this problem, preferably without a slow reflection.

It's not possible. You have redefined or over-ridden the two methods in
Class B.
 
M

Matt Humphrey

Hello,

How can I call a method of a base class from a base method whereby the
called method is redefined in a class that extends the base class

To be more concrete, assume the following classes
Class A {
void methodX() {
do_1;
methodY(); // **
}
void methodY() {
do_2;
}
}

Class B extends A {
void methodX() {
super.methodX();
methodY();
}
void methodY() {
do_3;
}
}

If you create an instance of Class B and call methodX on it, do_1 is
executed and next do_3 two times (once called by MethodX of Class A
and once by MethodX of class B). However the first time (line **) I
want to call methodY of class A, in order to get sequence do_1, do_2,
do_3.

Already tried to replace line ** to
((A)this).methodY();
but this doesn't work as the explicit cast to class A of an instance
of class B results in an instance of class B...

How can I solve this problem, preferably without a slow reflection.

The normal way for a base class to choose functionality based on the
subclass of the receiver is to use inheritance, but the overriding is
limited by how the methods intertwine. It's really all a matter of whether
you can refactor what you have already have. For example, if you change
class B to

classB {
// Do not override methodX
void methodY () {
super.methodY ();
do_3;
}
}

calling b.methodX () will result in do_1, do_2, and do_3 without changing
class A. But does that refactoring preserve the meaning of B.methodY? If it
doesn't you may have refactor class A to break methodY into an elemental,
non-overridden part and the inheritable part. You'll have to say further
what the refactoring constraints are--they're probably specific to the
problem and what you really need to do is to figure out what the inheritable
split between the two classes is.

Matt Humphrey http://www.iviz.com/
 
P

Piotr Kobzda

Class A {
void methodX() {
do_1;
methodY(); // **
}
void methodY() {
do_2;
}
}
[...]

How can I solve this problem, preferably without a slow reflection.

Try the following:

Class A {
void methodX() {
do_1;
methodY0();
}
void methodY() {
methodY0();
}
private void methodY0() {
do_2;
}
}


piotr
 
C

Curt Welch

Hello,

How can I call a method of a base class from a base method whereby the
called method is redefined in a class that extends the base class

To be more concrete, assume the following classes
Class A {
void methodX() {
do_1;
methodY(); // **
}
void methodY() {
do_2;
}
}

Class B extends A {
void methodX() {
super.methodX();
methodY();
}
void methodY() {
do_3;
}
}

If you create an instance of Class B and call methodX on it, do_1 is
executed and next do_3 two times (once called by MethodX of Class A
and once by MethodX of class B). However the first time (line **) I
want to call methodY of class A, in order to get sequence do_1, do_2,
do_3.

Already tried to replace line ** to
((A)this).methodY();
but this doesn't work as the explicit cast to class A of an instance
of class B results in an instance of class B...

How can I solve this problem, preferably without a slow reflection.

Kind regards,
Robbie De Sutter

There are a thousand ways to do it. Here's one:

class B {
methodX() {
do_1;
do_2;
do_3;
}
}

:)

(I know it's not what you need by without more details there isn't much
more to be said).

If you need do_1, do_2, and do_3 to be done in that order, you designed the
code wrong. It's not an issue of how the language works.

If you overide MethodY in the subclass, then you are saying you have
redefined MethodY and you want the super class to use your MethodY instead
of his MethodY. If you didn't want that to happen, you shouldn't have
overriden MethodY. Without being more specific about what your code is
doing, we can't give you much more advice about how to refracture it
correctly.

Maybe the solution is that you shouldn't be subclassing A in the first
place.

Maybe you should be using the has-a relationship instead of the is-a
relationship. Maybe A and B should inherit from a common interface which
defines MethodX and MethodY and class B should simply have a reference to
the Class A object. In which case the code works like this:

interface XY {
void methodX();
void methodY();
}

Class A implements XY {
void methodX() {
do_1;
methodY(); // **
}
void methodY() {
do_2;
}
}

Class B implements XY {
XY objectA;
B(XY a) {
objectA = a;
}
void methodX() {
objectA.methodX();
methodY();
}
void methodY() {
do_3;
}
}

That would make it do what you want but keep a similar structure to what
you already have. Just a thought.
 
R

robbie.desutter

Thank you all for the very useful feedback.

I see several solutions which could work, especially the solution of
Piotr as I can keep the extend relationship. And if that doesn't work,
Curt's solution is also a way to go, but than there is no extend
relationship anymore (Curt your assumption that Classes A and B
implement an interface was correct!).

Thank again!

Robbie De Sutter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top