Referencing the base method  
Author Message
halfpeaw





PostPosted: 2006-7-17 23:02:00 Top

java-programmer, Referencing the base method 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





PostPosted: 2006-7-17 23:11:00 Top

java-programmer >> Referencing the base method email***@***.com wrote:
> 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()

String text = super.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
>
 
Fred Kleinschmidt





PostPosted: 2006-7-17 23:29:00 Top

java-programmer >> Referencing the base method
<email***@***.com> wrote in message
news:email***@***.com...
>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
>
super.getText()

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


 
 
Jochen Schulz





PostPosted: 2006-7-18 17:54:00 Top

java-programmer >> Referencing the base method * email***@***.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.
--
I start many things but I have yet to finish a single one.
[Agree] [Disagree]
<http://www.slowlydownward.com/NODATA/data_enter2.html>
 
 
EJP





PostPosted: 2006-7-18 19:26:00 Top

java-programmer >> Referencing the base method Jochen Schulz wrote:

> 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





PostPosted: 2006-7-18 20:52:00 Top

java-programmer >> Referencing the base method * EJP:
> Jochen Schulz wrote:
>
>> 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.

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





PostPosted: 2006-7-19 13:52:00 Top

java-programmer >> Referencing the base method EJP wrote:
> 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.