-3

I had a scenario like this,

 String className;

 if(someCondition){

 className="A";

   }
 else{
   className='B'
  }

Now i want to do this dynamically

 className obj=(className) dbObj;//i am typcasting the db casting to particular class

Note:Here A and B classes having same setters and getters but belongs to two different tables in db

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Surya Prakash Tumma
  • 2,153
  • 4
  • 27
  • 47

1 Answers1

0

You should modify your design a littel bit.

Both the classes A and B should implement same inteface.

interface IAB {
}

class A implements IAB {
}

class B implements IAB {
}

Now, change your code as following:

IAB className;

if(someCondition) {
    className = (A) dbObj;
}
else {
    className = (B) dbObj;
}

OR

classname = (someCondition ? A : B) dbObj;
Azodious
  • 13,752
  • 1
  • 36
  • 71