0

Problem : I have multiple classes and I have received object of type "Object" in some method along with string which mentions the name of class to which that Object belongs. I need to typecast the Object that I received to one of the class type based on String.

Example :

public class Cat
{
  // Having Methods and Variables
}

public class Dog
{
  // Having Methods and Variables
}

Now following method in some other class will be called :

public void Method1(Object o, String s)
{
   Method2((Typecast based on value in s)o);  // s can be "Cat" or "Dog"

}

Method2 is overloaded i.e there are two types of it :

public void Method2(Dog g)
{

}

public void Method2(Cat c)
{

}

0 Answers0