Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1549 > unrolled thread

Interchanging objects?

Started byDavej <galt_57@hotmail.com>
First post2012-02-04 12:02 -0800
Last post2012-02-07 08:09 -0500
Articles 7 — 4 participants

Back to article view | Back to comp.lang.java.help


Contents

  Interchanging objects? Davej <galt_57@hotmail.com> - 2012-02-04 12:02 -0800
    Re: Interchanging objects? Lew <lewbloch@gmail.com> - 2012-02-04 23:00 -0800
      Re: Interchanging objects? Davej <galt_57@hotmail.com> - 2012-02-05 07:41 -0800
        Re: Interchanging objects? Lew <lewbloch@gmail.com> - 2012-02-05 11:34 -0800
        Re: Interchanging objects? Roedy Green <see_website@mindprod.com.invalid> - 2012-02-08 22:42 -0800
          Re: Interchanging objects? Lew <lewbloch@gmail.com> - 2012-02-09 09:21 -0800
    Re: Interchanging objects? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-07 08:09 -0500

#1549 — Interchanging objects?

FromDavej <galt_57@hotmail.com>
Date2012-02-04 12:02 -0800
SubjectInterchanging objects?
Message-ID<bb014e7e-2bf1-4d55-8677-6e2514826da5@p7g2000yqk.googlegroups.com>
If two objects have the same method names, can one of them be cast as
the other?

[toc] | [next] | [standalone]


#1550

FromLew <lewbloch@gmail.com>
Date2012-02-04 23:00 -0800
Message-ID<2362859.192.1328425259754.JavaMail.geo-discussion-forums@prhb20>
In reply to#1549
Davej wrote:
> If two objects have the same method names, can one of them be cast as
> the other?

Not unless they are type compatible. Method names do not define type 
compatibility - in fact, doing it by name is the antithesis of type safety.

Read up on the conversion rules here:
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#1551

FromDavej <galt_57@hotmail.com>
Date2012-02-05 07:41 -0800
Message-ID<9f687d98-30d7-4838-b09d-5ff6a77139b9@1g2000yqv.googlegroups.com>
In reply to#1550
On Feb 5, 1:00 am, Lew <lewbl...@gmail.com> wrote:
> Davej wrote:
> > If two objects have the same method names, can one of them be cast as
> > the other?
>
> Not unless they are type compatible. Method names do not define
> type compatibility - in fact, doing it by name is the antithesis
> of type safety.
>
> Read up on the conversion rules here:http://java.sun.com/docs/books/jls/third_edition/html/conversions.html
>
> --
> Lew

I have been trying to think of a convenient way to interchange objects
that provide calculations that are done slightly differently. The
objects all have the same method names and return the same types. Is
there an elegant way to accomplish this? Thanks.

[toc] | [prev] | [next] | [standalone]


#1552

FromLew <lewbloch@gmail.com>
Date2012-02-05 11:34 -0800
Message-ID<8494969.1891.1328470471486.JavaMail.geo-discussion-forums@prnc3>
In reply to#1551
Davej wrote:
> I have been trying to think of a convenient way to interchange objects
> that provide calculations that are done slightly differently. The
> objects all have the same method names and return the same types. Is
> there an elegant way to accomplish this? Thanks.

Yes. Instead of thinking in terms of objects, think in terms of types.

You have some concept of a type that will perform certain behaviors, the 
details of which will be handled by different specific subtypes of that type.

So define your overall type as in interface and have your specific subtypes 
implement that interface. Something along the lines of (not complete code):

 public interface Schmoo
 {
   Spronk schmooze(Scronx scronx);
 }

Now let's say you have two different kinds of 'schmooze()' you want types to 
perform.

 public class TankerSchmoo implements Schmoo
 {
   @Override public Spronk schmooze(Scronx scronx)
   {
     // an implementation that somehow involves Tankers
   }
 }

and

 public class HonkerSchmoo implements Schmoo
 {
   @Override public Spronk schmooze(Scronx scronx)
   {
     // an implementation that somehow involves Honkers
   }
 }

Mind you, you cannot convert TankerSchmoo to HonkerSchmoo or vice versa, but 
you can refer to either one as a Schmoo.

 Schmoo aSchmoo = new TankerSchmoo();
 Schmoo anotherSchmoo = new HonkerSchmoo();

You can have a collection of such things.

 List<Schmoo> schmoos = Arrays.asList(aSchmoo, anotherSchmoo);

and 

 Scronx scronx = getScronx();
 for (Schmoo schmoo : schmoos)
 {
   schmoo.schmooze(scronx);
 }

But what you need to do is study the Java tutorials and the conversion rules to 
which I've already alluded, and understand inheritance and polymorphism.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#1557

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-02-08 22:42 -0800
Message-ID<0iq6j7t80gambj8ck9nbpldtscdes3d6pv@4ax.com>
In reply to#1551
On Sun, 5 Feb 2012 07:41:13 -0800 (PST), Davej <galt_57@hotmail.com>
wrote, quoted or indirectly quoted someone who said :

>I have been trying to think of a convenient way to interchange objects
>that provide calculations that are done slightly differently. The
>objects all have the same method names and return the same types. Is
>there an elegant way to accomplish this? Thanks.

This is what an interface is for.  All the classes that perform the
same operations but in a different way all implement the same
interface.  Then you can refer to any objects of any of those classes
by their interfacename.  What you call myinterface.calcBiggest for
example, each object will uses its class's version of calcBiggest.

See http://mindprod.com/jgloss/interface.html
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
One of the most useful comments you can put in a program is 
"If you change this, remember to change ?XXX? too".
 

[toc] | [prev] | [next] | [standalone]


#1558

FromLew <lewbloch@gmail.com>
Date2012-02-09 09:21 -0800
Message-ID<6097014.139.1328808112771.JavaMail.geo-discussion-forums@pbcxd5>
In reply to#1557
Roedy Green wrote:
> Davej wrote, quoted or indirectly quoted someone who said :
> >I have been trying to think of a convenient way to interchange objects
> >that provide calculations that are done slightly differently. The
> >objects all have the same method names and return the same types. Is
> >there an elegant way to accomplish this? Thanks.
> 
> This is what an interface is for.  All the classes that perform the
> same operations but in a different way all implement the same
> interface.  Then you can refer to any objects of any of those classes
> by their interfacename.  What you call myinterface.calcBiggest for
> example, each object will uses its class's version of calcBiggest.
> 
> See http://mindprod.com/jgloss/interface.html

After you understand how interfaces work to encapsulate common types of 
behavior (as opposed to common implementations of behavior), you can use Java 7 
"closures" effectively. The Java version of closures is syntactic sugar for 
single-abstract-method (SAM) interfaces, that is, interfaces that define 
exactly one abstract method.

Example:

 public interface Closure
 {
   int performOp(int x, int y);
 }

The operations performed by 'performOp()' can be anything that operates on two 
'int' values and returns an 'int'.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#1554

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-02-07 08:09 -0500
Message-ID<jgr7r2$cn9$1@dont-email.me>
In reply to#1549
On 2/4/2012 3:02 PM, Davej wrote:
> If two objects have the same method names, can one of them be cast as
> the other?

     No, not unless one subclasses the other and the cast is "upward."
The fact that Pistol and Petunia both have shoot() methods does not
imply that one can be treated as the other.

     If the methods in question are specified by an interface and both
classes implement that interface, then an instance of either class can
be treated as an "instance" of the interface:

	interface Portable {
	    void carry();
	}

	class Luggage implements Portable {
	    void carry() { ... };
	}

	class Tune implements Portable {
	    void carry() { ... };
	}

In a situation like this, you can do:

	Portable p1 = new Luggage();
	Portable p2 = new Tune();
	Portable p3 = choose() ? p1 : p2;
	p3.carry();

... and so on, without so much as a cast.  But you still can't
convert a Luggage to a Tune or vice versa; you can only use the
fact that both classes are Portable.

     Something very similar can be done if the methods are specified
by a common ancestral class (perhaps an abstract class):

	class SportsFan {
	    void cheer() { ... };
	}

	class GiantsFan extends SportsFan {
	    @Override void cheer() { ... };
	}

	class PatriotsFan extends SportsFan {
	    @Override void cheer() { ... };
	}

	SportsFan f1 = new GiantsFan();
	SportsFan f2 = new PatriotsFan();
	SportsFan f3 = choose() ? f1 : f2;
	f3.cheer();

GiantsFan instances and PatriotsFan instances are all SportsFan
instances and can be treated as such, but you still can't make a
GiantsFan of a PatriotsFan.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web