• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

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
0
pkrish80
Asked:
pkrish80
  • 11
  • 7
1 Solution
 
for_yanCommented:
yes you can use instanceof operator for that purpose
0
 
for_yanCommented:

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

}

}
0
 
for_yanCommented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
pkrish80Author Commented:
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.
0
 
for_yanCommented:


Do you have such method:  getListOfPayRollDept();

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

Does it say this way:

List<Department>  getListOfPayRollDept() {
////
}
0
 
for_yanCommented:

If so, then yes you can write like that:

List<PayrollDept> pdtList = getListOfPayRollDept();

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

0
 
pkrish80Author Commented:
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
0
 
for_yanCommented:

 I think you can do what you are doing - I don't see any problems.
0
 
pkrish80Author Commented:
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
0
 
for_yanCommented:
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
0
 
pkrish80Author Commented:
Can't I do list.contain(PayrollDept.getClass)?
0
 
for_yanCommented:
No, you cannot do it - conatin looks for aprticular element not for class
0
 
for_yanCommented:
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

0
 
pkrish80Author Commented:
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
0
 
for_yanCommented:
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
0
 
pkrish80Author Commented:
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?
0
 
for_yanCommented:
Well, I think, that depends on how many differences there are between these two classes,
but once you decide still to keep only one class ReportWriter, it seems more logical to me
if that reportWriter would accept the object of the super class, I guess that would be  Department
  and inside ReportWriter you would deal with the common part (i hope it exists) and also
establish what is the particular instance and deal with specific parts depending
on what particular instance you have.
0
 
pkrish80Author Commented:
I am passing the Department reference to the ReportWriter. I have common part that exists in the parent class.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now