Suppose that there are classes X,Y,Z and each of these is having a method hello(). Now, I have another class A which is having a method sayHello(String args). Now, I will send any of the three class names to the above method as a String and want to create that particular object and call hello method on it.
If I call new A().sayHello("x"), object of x must be created and x.hello() must be called. the same thing shud happen with y and z..
How to implement this method?
I just took 3 classes x y z 4 timebeing. There may be more classes in some instances. So,simple if-else, or switch condition will not be a better idea I think.
Any reasearch or suggestions in this regard are appreciated.
Hmm. What you are doing is a little confused. Why do you have to pass your X, Y and Z objects as a String? Why can't you pass them as generic Objects?
I think this might be what you want to do - create three classes (your X, Y and Z classes) and have them implement an interface. Define your hello() method in this interface and implement it in your classes. Now you can call your sayHello() method without caring which type of object you have. No need for a conditional.
Just in cas eyou're interested, there's a pretty good example of this in the book Head First Design Patterns
As Paul said, the first thing you need is a interface for the classes X, Y and Z to implement so we can treat them the same according to a common behaviour. I have some doubts on your method signature (why are you passing the class name as the parameter??) but we'll do it your way...
First step: if the method sayHello was passed an instance of a concrete class, eg X, it would look like this:
Second step: if we make sayHello accept the interface instead:
Third step: if we make sayHello accept a Class type and the Class implements the HelloInterface:
Fourth step: if we make sayHello accept a Class name and the Class implements the HelloInterface:
As you can see I haven't done everything for you, java.lang.Class does most of the missing stuff. Obviously given your requirements you need the fourth step, but if you read the first chapter of the book above you'll understand what I'm referring to.
author and iconoclast
Thank you so much for all the responses. I will explain the scenario in a more detailed way. Please be patient and go thru it.
I have a log generator program which generates different logs very rapidly. I have to catch each log and depending on the type of a pariticualr value in it, I need to insert it into a particular table in the database. Like that, I have to insert logs into more than 40 different tables.
I have written an xml file which maps each type of the log to a particualr class name, instance of which will be resposible for inserting the log into database.
So, I need to implement a program which takes the log from generator program, determines the type of it, gets the appropriate class from the XML mapping,creates an object of it and call the method insert(LogStream) on it.
Every class will have this insert method with the same signature but with a different implementation.
I could implement all the functionality except creating the Objects based on the String that I get from the XML file.
Thank you so much for your patience and help.
God is real unless declared integer...Kala.
author and iconoclast