We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Dynamic casting (Read 1496 times)
Dynamic casting
Nov 24th, 2009, 9:19am
 
Hi,

From another sketch, I receive a UDP message like "off", "standby", "show". When I receive one of those messages, I want to call the update() function of the appropriate object, related to the message.

I did an example. The msg variable is the receive data from UDP, hardcoded for the exemple. I highlight the line where I want to do dynamic cast. Is that possible or does any other way exist to do something like that ?

Thanks you,

Code:
Hashtable animation = new Hashtable();
void setup(){
 msg="off"
 animation.put("off", new Anim1());
 animation.put("standby", new Anim2());
 Anim objtemp = (Anim)animation.get(msg); // Here is the line to change for dynamic
 objtemp.update();
}

class Anim1 {
 void update(){    
   rect(random(122), random(98), random(120), random(84));
 }
}

class Anim2 {
 void update(){
   ellipse(random(200), random(200), random(50), random(50) );
 }
}

Re: Dynamic casting
Reply #1 - Nov 24th, 2009, 12:44pm
 
I changed your code so that it compiles and runs.
Is this what you meant?

Code:
Hashtable animation = new Hashtable();
void setup(){
 String msg="off";
 animation.put("off", new Anim1());
 animation.put("standby", new Anim2());
 Anim objtemp = (Anim)animation.get(msg); // Here is the line to change for dynamic
 objtemp.update();
}
interface Anim
{
 void update();
}
class Anim1 implements Anim{
 void update(){    
   rect(random(122), random(98), random(120), random(84));
 }
}

class Anim2 implements Anim {
 void update(){
   ellipse(random(200), random(200), random(50), random(50) );
 }
}
Re: Dynamic casting
Reply #2 - Nov 25th, 2009, 7:29am
 
I answer myself hehe !

Thank again but if you have some docs on interface and implements, I'll appreciate.

Here's the final code.
Code:

Hashtable animation = new Hashtable();
void setup(){
 String msg="standby";
 animation.put("off", new Anim1("vline"));
 animation.put("standby", new Anim2("hline"));
 Anim objtemp = (Anim)animation.get(msg);
 objtemp.update();
}
interface Anim
{
 void update();
}

class Base{
 String name;
}

class Anim1 extends Base implements Anim{
 Anim1(String n){
   name = n;
 }
 void update(){    
   rect(random(11), random(11), random(120), random(84));
 }
}

class Anim2 extends Base implements Anim{
 Anim2(String n){
   name = n;
 }
 void update(){
   ellipse(random(11), random(11), random(50), random(50) );
 }
}  

Re: Dynamic casting
Reply #3 - Nov 25th, 2009, 8:19am
 
Interfaces are a kind of contract: classes implementing it must implement the methods of the interface.
So when you get an object typified by an interface, you are sure it has a given set of methods.
Note an object can implement several interfaces.

On the other hand, you can make a class to inherit from (extend) another. The interest is that you can define methods common to all derived classes (default implementation) and derived classes can override these methods to do their own.

The parent class can be abstract, meaning you can't create a new instance of it. Eg. the abstract class 'Fruit' is extended to 'Banana' and 'Apple': you can create an apple but not a fruit, too abstract.

You can do:
Code:
interface Anim
{
void update();
}

abstract class Base{
String name;
Base(String n){
name = n;
}
}

class Anim1 extends Base implements Anim{
Anim1(String n){
super(n);
}
void update(){
rect(random(11), random(11), random(120), random(84));
}
}

class Anim2 extends Base implements Anim{
int an;
Anim2(String n){
super(n);
an = n.length();
}
void update(){
ellipse(random(11), random(11), random(50), random(50) );
}
}

or even just drop the interface:
Code:
abstract class Anim{
String name;
Anim(String n){
name = n;
}
abstract void update();
}

class Anim1 extends Anim{
Anim1(String n){
super(n);
}
void update(){
rect(random(11), random(11), random(120), random(84));
}
}

class Anim2 extends Anim{
int an;
Anim2(String n){
super(n);
an = n.length();
}
void update(){
ellipse(random(11), random(11), random(50), random(50) );
}
}
Re: Dynamic casting
Reply #4 - Nov 25th, 2009, 11:04am
 
I have to say I found the abstract class construct really useful; particularly when you take into account the fact that you can define methods as abstract (i.e. which must be implemented in the inheriting class)...

If all child classes inherit from the same base class is there any advantage to also implementing an interface, rather than simply declaring a class and method as abstract?  To me the latter seems like a better solution since otherwise you rely on people implementing the interface in their class declaration to ensure a method is defined; which doesn't seem especially 'reliable'...
Re: Dynamic casting
Reply #5 - Nov 25th, 2009, 4:17pm
 
Quote:
otherwise you rely on people implementing the interface in their class declaration to ensure a method is defined; which doesn't seem especially 'reliable'

Well, if you don't implement the interface, you cannot use the class in a generic way, like in the first answer. So it is still mandatory.

Interface vs. abstract class: it is a bit conceptual. There is lot of literature on the topic, eg. interface vs abstract class.

Somehow, an interface defines a behavior, a capability, a contract. A class can inherit several classes: eg. it can be a Map, can be Serializable, and can be Clonable.
An abstract class is deeper, it is a parent-child relation. By telling you extends an abstract class, you define a "is-a" relation: a Circle is-a Shape, a Lion is-a Creature, etc.
Both relations have their uses.

Note: lot of people think one should favor has-a relation, ie. composition, declaring objects in the class to get their properties, preferably to inheritance. A tendency is to say to reserve inheritance to frameworks, base classes, libraries, etc., but to keep it to minimum in end programs.
Page Index Toggle Pages: 1