Problem: With the following classes, I am trying to select a base class at runtime depending upon an instance variable of parent class in Java. I believe there should be some fundamental property in OOPS which can help me achieve it which I am just missing.
class Transaction{
int id;
enum transactionType;
}
class ATypeTransaction extends Transaction{
/* bunch of instance variable specific to ATypeTransaction */
}
class BTypeTransaction extends Transaction{
/* bunch of instance variable specific to BTypeTransaction */
}
My Approach: I am trying to use something like this (static polymorphism within switch-case). The problem with this approach is I am still not able to get hold of child class. Rather I am getting parent class which stops me from accessing child class attributes.
public Transaction selectTransaction(TransactionType transactionType){
Transaction transaction;
switch(transactionType) {
case = "AType":
transaction= new ATypeTransaction();
break;
case = "BType":
transaction= new BTypeTransaction();
break;
}
return transaction;
}
Please help me out with this problem and provide a good design to solve this. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire