-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

0

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.