Adding a method

B

bruce

Is it possible to a method that "Looks" like an method supplied from a
Java Library?

For example, I know there is a method to get text
(txtName.setText("Joe").

What I would like to do is write a method,
txtName.setMyMethod(argument). I know I can write
private void setMyMethod(argument) but I was "wondering" if I could do
it the other way.

Just a curiosity question...

Thanks..

Bruce
 
A

Arne Vajhøj

Is it possible to a method that "Looks" like an method supplied from a
Java Library?

For example, I know there is a method to get text
(txtName.setText("Joe").

What I would like to do is write a method,
txtName.setMyMethod(argument). I know I can write
private void setMyMethod(argument) but I was "wondering" if I could do
it the other way.

No.

Java does currently not support extension methods.

Arne
 
M

markspace

Is it possible to a method that "Looks" like an method supplied from a
Java Library?


"Is it possible to a method..."

I think you left some English grammar out of that phrase there.

What I would like to do is write a method,
txtName.setMyMethod(argument). I know I can write
private void setMyMethod(argument) but I was "wondering" if I could do
it the other way.


Since I'm not sure what you are asking (what the heck is an extension
method anyway?), it's hard to answer.

class Txt {
public void setText( String t ) {}
}

class MyTxt extends Txt {
public void setMyMethod( Object a ) {}
}

This is the classic way of extending a class in OO design. Is this what
you have in mind?
 
P

projectmoon

(what the heck is an extension method anyway?)

In C#, an extension method is syntactic sugar that allows you to use a
static method in the same way as an instance-based method. So, while it
is declared static, it is called with instance.method().

Java does not support this kind of feature, and I doubt it ever will.
 
G

Graham Cox

projectmoon said:
In C#, an extension method is syntactic sugar that allows you to use a
static method in the same way as an instance-based method. So, while it
is declared static, it is called with instance.method().

Java does not support this kind of feature, and I doubt it ever will.

I missed the start of the thread, but Java lets you call static methods on
instances of a class. The compiler (or is it just Netbeans?) warns against
doing so but it works just fine...

(Apologies if that is irrelevant to the original question)
 
P

projectmoon

I missed the start of the thread, but Java lets you call static methods
on instances of a class. The compiler (or is it just Netbeans?) warns
against doing so but it works just fine...

(Apologies if that is irrelevant to the original question)

Yeah, you can call static methods from an instance in Java, but they're
still static. C# extension methods take an object as their first
parameter that uses the "this" keyword in its signature, i.e.:

public static void ExtensionMethod(this List list, int number) { ... }

You can then call the extension method with the same syntax as an
instance-level method. It's sort of a ghetto inheritance. It doesn't give
you special access to the class's private state, but it allows you to
"expand" the class to your needs.
 
A

Arne Vajhøj

Since I'm not sure what you are asking (what the heck is an extension
method anyway?), it's hard to answer.

There is a good chance that it is a C# style extension method.

A simple example.

Let us say that you don't like to write:

myintvariable.ToString("X")

and want to use:

myintvariable.ToHexString()

then you just add a:

public static class MyExtensions
{
public static string ToHexString(this int o)
{
return o.ToString("X");
}
}

and voila it works.

The example is rather pointless, but it is a way
to extend existing frameworks in a very non-intrusive way.

It can also make it a bit hard to figure where the code
actually come from.

Arne
 
M

markspace



I don't think "Extensions" and Defender Methods are the same thing.
Extensions, as mentioned, are syntactic sugar for static methods.

Defender Methods are totally different. Given some interface:

interface A {
T method( argument_list );
}

It is impossible to add an new method to this interface without breaking
each and every class that implements this interface. In a public API
(like Java's API) that means one can never ever add a method to the
interface. Defender methods allow the interface to have a new new
method added, and also to have a static implementation so that any
existing classes will not break, and have a valid default
implementation. This sounds the opposite of "sugar" to me.
 
A

Arne Vajhøj

I don't think "Extensions" and Defender Methods are the same thing.
Extensions, as mentioned, are syntactic sugar for static methods.

Defender Methods are totally different. Given some interface:

interface A {
T method( argument_list );
}

It is impossible to add an new method to this interface without breaking
each and every class that implements this interface. In a public API
(like Java's API) that means one can never ever add a method to the
interface. Defender methods allow the interface to have a new new method
added, and also to have a static implementation so that any existing
classes will not break, and have a valid default implementation. This
sounds the opposite of "sugar" to me.

They are very much the same.

In .NET framework they have been used extensively to
add functionality to certain interfaces especially
IEnumerable<T>.

The proposal quoted is more powerful but also
more complex than the .NET extension methods
though.

And the proposal call the defender method for
"virtual extension methods".

Arne
 
M

markspace

They are very much the same.

In .NET framework they have been used extensively to
add functionality to certain interfaces especially
IEnumerable<T>.


Then I don't think that Extension methods in .NET are like the
previously discussed C# extension methods.

And the proposal call the defender method for
"virtual extension methods".


Emphasis on the word "virtual." That's the whole point, to add a method
to the virtual dispatch mechanism where one could not before.
 
L

Lew

markspace said:
It is impossible to add an new method to this interface without breaking
each and every class that implements this interface. In a public API
(like Java's API) that means one can never ever add a method to the
interface.

That didn't stop Sun from doing just that, however. IIRC it was 'ResultSet'
that picked up a couple of methods in Java 6, and maybe others.
 
R

Roedy Green

Is it possible to a method that "Looks" like an method supplied from a
Java Library?

What you have to do is create another class that extends the class
that contains the method you want to replace. Your method can even
user the original method with super.xxxx(...).

You can be thwarted by private methods or private classes.

This is a very basic feature of Java. You need a textbook to teach
you all this stuff. Even an out of date one will do.

See http://mindprod.com/jgloss/gettingstarted.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

Microsoft has a new version out, Windows XP, which according to everybody is the "most reliable Windows ever." To me, this is like saying that asparagus is "the most articulate vegetable ever."
~ Dave Barry
 
J

jlp

Le 14/10/2010 02:11, bruce a écrit :
Is it possible to a method that "Looks" like an method supplied from a
Java Library?

For example, I know there is a method to get text
(txtName.setText("Joe").

What I would like to do is write a method,
txtName.setMyMethod(argument). I know I can write
private void setMyMethod(argument) but I was "wondering" if I could do
it the other way.

Just a curiosity question...

Thanks..

Bruce
You can use byte code injection by using AspectJ.
http://www.eclipse.org/aspectj/

but it is an extension of Java. You can weave the method at Compile Time
or at Load Time.
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top