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 / July 2006

Tip: Looking for answers? Try searching our database.

Referencing the base method

Thread view: 
halfpeaw@gmail.com - 17 Jul 2006 16:02 GMT
I am creating a new custom JTextField object. I am overwriting my
existing getText() method inside of it.  The problem I am having is I
don't know to get the text from the actual text field while I'm in my
new getText() method.  I tried casting my object but that didn't work.
so my overall code looked something like this:

public Class CustJTextField extends JTextField {

  private int length;

  CustJTextField(int length) {
      this.length = length
   }

  public String getText() {
     String text = ((JtextField)this).getText()
     if (text.length() > length) {
        return null;
     }
     return text
  }
}

Thats a much simpler form of what I'm coding, but you should be able to
get the idea.  Unfortunately calling the getText method with in my
custom GetText method, ends up with a stack overflow error.  Is there
some way to reference the base method?  Thanks for any help you guys
can provide
Rogan Dawes - 17 Jul 2006 16:11 GMT
> I am creating a new custom JTextField object. I am overwriting my
> existing getText() method inside of it.  The problem I am having is I
[quoted text clipped - 12 lines]
>    public String getText() {
>       String text = ((JtextField)this).getText()

        String text = super.getText();

>       if (text.length() > length) {
>          return null;
[quoted text clipped - 8 lines]
> some way to reference the base method?  Thanks for any help you guys
> can provide
Fred Kleinschmidt - 17 Jul 2006 16:29 GMT
>I am creating a new custom JTextField object. I am overwriting my
> existing getText() method inside of it.  The problem I am having is I
[quoted text clipped - 24 lines]
> some way to reference the base method?  Thanks for any help you guys
> can provide

super.getText()

Signature

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Jochen Schulz - 18 Jul 2006 10:53 GMT
* halfpeaw@gmail.com:

> public Class CustJTextField extends JTextField {
-- snip
>    public String getText() {
>       String text = ((JtextField)this).getText()

This doesn't work for a good reason. The virtual machine always knows
the real class of an object, no matter what you cast it to. And if you
call a method on an object, the VM always calls the method of that name
in the class that your object is a type of (and if it isn't there, it
searches up the hierarchy). This is a part of polymorphism, one of the
core concepts of object orientated programming.

Imagine you would be passed an object of the type ArrayList in one of
your methods, but your method signatures just declares it to be a
Collection. If your way of trying things above worked, the VM would have
to throw an exception, because Collection is just an interface with no
implementation for its declared methods.

J.
Signature

I start many things but I have yet to finish a single one.
[Agree]   [Disagree]
                <http://www.slowlydownward.com/NODATA/data_enter2.html>

EJP - 18 Jul 2006 12:24 GMT
> This doesn't work for a good reason. The virtual machine always knows
> the real class of an object, no matter what you cast it to. And if you
> call a method on an object, the VM always calls the method of that name
> in the class that your object is a type of (and if it isn't there, it
> searches up the hierarchy). This is a part of polymorphism, one of the
> core concepts of object orientated programming.

This isn't really a good reason, and it certainly has nothing to do with
the core concept of polymorphism. The equivalent code would have worked
in C++ because the cast would have adjusted the object address, which
would have adjusted the effective vtable used. In Java the cast doesn't
do that, but it does do a real typecheck.

Just accept it as a difference between the two languages.
Jochen Schulz - 18 Jul 2006 13:52 GMT
* EJP:

>> This doesn't work for a good reason. The virtual machine always knows
>> the real class of an object, no matter what you cast it to. And if you
[quoted text clipped - 5 lines]
> This isn't really a good reason, and it certainly has nothing to do with
> the core concept of polymorphism.

Oh, really? (I ask because I am interested, not because I think I am
right and you are wrong).

I thought a classic example of polymorphism goes like this:

Say you have a base class (or an interface) <Widget> and a few derived
classes <Button>, <ScrollBar> etc. A method paint() is part of the
<Widget> interface which all descendants of <Widget> have to implement.
That way a <Window> object may tell all <Widget> instances it knows
about to draw themselves by calling their paint() method (and maybe
passing itself as a parameter).

As fas as I can tell, this is polymorphism which wouldn't be possible
when "up"casting an object to one of its baseclasses would later on make
method lookups start from that baseclass.

Or is my assumption wrong that passing an object to a method which takes
an instance of a baseclass of this object's class as a parameter is
equivalent to upcasting?

> The equivalent code would have worked in C++ because the cast would
> have adjusted the object address, which would have adjusted the
> effective vtable used. In Java the cast doesn't do that, but it does
> do a real typecheck.

Scary. (For someone who has never touched neither C nor C++.)

J.
Signature

I wish I could achieve a 'just stepped out of the salon' look more
often. Or at least once.
[Agree]   [Disagree]
                <http://www.slowlydownward.com/NODATA/data_enter2.html>

EJP - 19 Jul 2006 06:52 GMT
> The equivalent code would have worked
> in C++ because the cast would have adjusted the object address, which
> would have adjusted the effective vtable used.

Thinking about this overnight I've concluded that it is entirely
possible that I have forgotten everything I ever knew about C++ vtabs
ten years ago, so the above may well be complete BS.


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.