Java : how to call methods with the same name in different objects instantiated from different classes?

I think that my question is not very clear :) Here is my problem (in more details):

I have got several objects (lets say objA, objB, objC, etc...) which are instances from different classes (lets say ClassA, ClassB, ClassC... respectively).

These objects A, B, C,..., call the same object (let's call it jsonConnector, instance of JsonConnector class) and I want then this jsonConnector to call the updateUI() method of the object by which it has been created.

Code in my ClassX (ClassA, ClassB, etc...) class:

JsonConnector jsonConnector = new JsonConnector(this);

Constructor of my JsonConnector class:

private Object callingObject;

public JsonConnector(Object aCallingObject) {

 callingObject = aCallingObject

Somewhere in my JsonConnector class, I want to do:


but I have got a compiling error "The method updateUI() is undefined for the type Object"

I could do :

String callingClassName = callingObject.getClass().getSimpleName();

if(callingClassName == "ClassA")
else if(callingClassName == "ClassB")
else if...

But it I don't want to write 100 'else if' statements !

Anybody knows how I can do ?

Thanks !!!


I think you could program to an interface.

public interface GuiUpdate
   void UpdateUI() ;
//do the same for classes that updates a gui. 
public class ClassA implements GuiUpdate 

Do you have control over JsonConnector? If so, inject the object references:

public JsonConnector(GuiUpdate updater)

To add, this means you can simply do the following in a subsequent method, polymorphism and abstraction at its finest:

  updater.UpdateUI() ;

Need Your Help

php get extract some values from multidimensional array

php arrays multidimensional-array

I have a multidimensional array which has 2-3 levels so i need to extract some data from

Rabin-Karp string search algorithm

c string-search rabin-karp

My previous question pertained to the general string search algorithm.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.