Referencing the base method

H

halfpeaw

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
 
R

Rogan Dawes

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();
 
F

Fred Kleinschmidt

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()
 
J

Jochen Schulz

* (e-mail address removed):
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.
 
E

EJP

Jochen said:
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.
 
J

Jochen Schulz

* EJP:
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.
 
E

EJP

EJP said:
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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top