Link to home
Start Free TrialLog in
Avatar of pkrish80
pkrish80

asked on

Getting properties of a child class using parent class reference

Hi,

I have a parent class, Department and two sub classes PayrollDept and HrDept

Department contains the common attributes between PayrollDept and HrDept

Class Department{
// Set of common attributes between PayrollDept and HrDept
protected int general code;
//setters and getters


}
So, I have
Class PayrollDept extends Department{
private String name,
private String description,
private int numDays;
private int paycheckCode

PayrollDept(String name, String description, int numDays, int payCheckCode, int generalCode){
super();
this.name = name;
this.description = description;
this.numDays = numDays;
this.paycheckCode = paycheckCode;
this.generalCode = generalCode;
}

// Setters and getters for the attributes in this class
}

Similar structure for Class HrDept.

I have a class, TestDeptRunner, where I pass in a list of departments to a method.

Class TestDeptRunner{

TestDeptRunner(List<Department> dptList){

}
checkDeptStat(dptList) {
// Here I would like to check if the dptList is of type PayrollDept and
// get the private variables of Payroll Dept
// I would like to do the same for HrDept

Is it possible to do that?

}

}

Class TestRunner{

// I am getting a list of PayrollDept objects from a sql object  and am getting a list of HrDept objects from a sql object
// I would like to pass them as a list of Department objects to the TestDeptRunner referenced
 // above. I will calling TestDeptRunner twice for that purpose. How can I do that effectively?
}

Thanks
Avatar of for_yan
for_yan
Flag of United States of America image

yes you can use instanceof operator for that purpose

for(Department dpt : dptList){

if(dpt instanceof PayrolDept){
ParrollDept pd = (PayrollDept) dpt;
int numDays = pd.getNumDays();

} else if (dpt instanceof HrDept){
HrDept hd = (HrlDept) dpt;
//get properties of HrDept

}

}
I didn't understand about calling twice.
You can have one list which will have elements of both types - so I don't think you need two lists
Avatar of pkrish80
pkrish80

ASKER

Thanks

I was thinking of instanceOf. The way you describe makes sense.

How do I set up my List of department objects in my specific to PayrollDept and HrDept objects;
Suppose,

List pdtList = getListOfPayRollDept();
List<Department> testDpt = new ArrayList(pdtList);

Can I do the above? Please let me know.


Do you have such method:  getListOfPayRollDept();

whiat is its signture - what type of object it returns?

Does it say this way:

List<Department>  getListOfPayRollDept() {
////
}

If so, then yes you can write like that:

List<PayrollDept> pdtList = getListOfPayRollDept();

List<Department> testDpt = new ArrayList<Department>(pdtList);

Currently, my method, getListOfPayRollDep() returns a List<PayrollDept>. I need to run some business rules on the list of List<PayrollDept> before passing a list of type department.

Should I return a type of List<Department>? Any other suggestions?

Thanks

 I think you can do what you are doing - I don't see any problems.
Another question I had was, say I had a list<Department> containing objects of type PayrollDept, Is there a specific api which can tell that list<Department> ls contains type of object PayrollDept without iterating through the list?

Thanks
No, there could not be such method, because list<Department> may conatain mixture of payrolDeptmnet or Hrdepartemnt and othe children of department
so you can check the forst element - but no guarantee that the second one is the same - if you are sure that they are all the same
just check element 0
Can't I do list.contain(PayrollDept.getClass)?
No, you cannot do it - conatin looks for aprticular element not for class
Can you explain what is your goal with it?

If you know that they are all of the same type
just chek the first element

if(list.get(0) instanceof PayrollDept) ...

if you have no guaratntee that they are all the same - there is
no other way but to chek each one

I know they are all of the same type and I am checking the first element using instanceof. So, that works for me.

I was wondering if there was any api which could give the type of list objects back.

Thanks
No, there cannot be such method, and if there were - it would have to go through all elememnnts
the same way you would go - so no sense to look for it very much.

It is like contains() for finding the actual  elemnts , but if you write it yoursellf - it will be the same thing - it still checking every elemnt
I have a question related to this problem.

In my Class TestDeptRunner, I am getting a list containing PayRollDept objects and HrDept objects.

Earlier my code only had PayRollDept objects. I also have a api call,

reportWriter.createEntry(payRollDept pd, pd.name, pd.description)

The reportWriter has two subclasses, CSV type of writer and XML type of writer and has createEntry methods specific to them.

Now, my reportWriter method also has to take type of HrDept with some attributes in it as parameters.

Should I check the type and instance of Department in TestDeptRunner class or defer it to the reportWriter implementation?

In that case I will be passing the super class of payRollDept and HrDept in the reportWriter method?
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am passing the Department reference to the ReportWriter. I have common part that exists in the parent class.