Using overload to implement the equivalent of a switch

E

Everyone

Hello!
I've been trying to use polymorphism -via- overloading+overriding to
simulate a dynamic switch

class AbstractBase{
public abstract void doSomething(Object anObject);
public void activate(Object anObject){
doSomething(anObject);
}
}

class ResponsibleChild{
public void doSomething(Object anObject){
System.out.println("Type not supported");
}
public void doSomething(Integer anInt){
System.out.println("Integered");
}
public void doSomething(String aString){
System.out.println("Strung");
}

}

class Main{
public static void main(String args[]){
AbstractBase reference = new ResponsibleChild();
reference.activate(Integer.valueOf(10));
reference.activate("Strung!!");
}
}

The output I expected from this was ;

Integered
Strung

However the actual output is ;
Type not supported
Type not supported

Shouldn't the call
a. be made in the context of the object, and
b. narrow to the best possible match for the method signature

?

Regards,
Abhishek
 
S

Stefan Ram

Everyone said:
I've been trying to use polymorphism -via-
overloading+overriding to simulate a dynamic switch

Overloading resolution is »static« insofar as
it happens at compile time - not run time.
class AbstractBase{
public abstract void doSomething(Object anObject);

This won't compile, because the class
is not declared with »abstract«.
AbstractBase reference = new ResponsibleChild();

This also should not compile, because
»ResponsibleChild« does not extend »AbstractBase«
by its declaration.

I recommend to write an SSCCE and then post this.
 
E

Everyone

Thanks Stefan, Eric

I'll try another approach and see if that may work.

Regards,
Abhishek
 
D

Daniel Pitts

Everyone said:
Hello!
I've been trying to use polymorphism -via- overloading+overriding to
simulate a dynamic switch

class AbstractBase{
public abstract void doSomething(Object anObject);
public void activate(Object anObject){
doSomething(anObject);
}
}

class ResponsibleChild{
public void doSomething(Object anObject){
System.out.println("Type not supported");
}
public void doSomething(Integer anInt){
System.out.println("Integered");
}
public void doSomething(String aString){
System.out.println("Strung");
}

}

class Main{
public static void main(String args[]){
AbstractBase reference = new ResponsibleChild();
reference.activate(Integer.valueOf(10));
reference.activate("Strung!!");
}
}

The output I expected from this was ;

Integered
Strung

However the actual output is ;
Type not supported
Type not supported

Shouldn't the call
a. be made in the context of the object, and
b. narrow to the best possible match for the method signature

?

Regards,
Abhishek
The compiler has to be able to statically tell which method signature
its trying to invoke, doSomething(Object) in your example. At runtime,
the VM will see that doSomething(Object) has been overridden by your
derived class, and invoke the derived instance.

What you are trying to do is something called multi-dispatch. Java does
not support this concept at the language level.
 

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,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top