Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
robbie.desutter@gmail.com - 03 Dec 2007 07:43 GMT
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
Lionel van den Berg - 03 Dec 2007 08:26 GMT
> Hello,
>
[quoted text clipped - 34 lines]
>
> 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.
Matt Humphrey - 03 Dec 2007 14:18 GMT
> Hello,
>
[quoted text clipped - 34 lines]
>
> 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/
Piotr Kobzda - 03 Dec 2007 15:29 GMT
> Class A {
>    void methodX() {
[quoted text clipped - 5 lines]
>    }
> }

[...]

> 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
Curt Welch - 03 Dec 2007 16:16 GMT
> Hello,
>
[quoted text clipped - 37 lines]
> 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.

Signature

Curt Welch                                            http://CurtWelch.Com/
curt@kcwc.com                                        http://NewsReader.Com/

robbie.desutter@gmail.com - 04 Dec 2007 09:11 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.