Forums Register Login

basic question on print object array

+Pie Number of slices to send: Send
Hi to all

It 'a logic problem, rather than of code... Explain better.
In a class A are brought to an array object that is a subclass of A.
In the graphic design of Sotware I see this method listEmployees that should print the information contained in the array.
How can I print this info if I can not access the information of the subclass as name, employee salary?
I assume then that I have to take this information only from the array, but how?

I appreciate your help
+Pie Number of slices to send: Send
Can the subclass provide a toString() method? Or getter?
+Pie Number of slices to send: Send
 

manu lix wrote:In a class A are brought to an array object that is a subclass of A.


I don't understand that statement, but an object of class A cannot be assigned to an array that is defined as being a subclass of A. ie, you can't write:
  SubclassOfA[] array = new SubclassOfA[10];
  array[0] = new A();

You can, of course, do the opposite though, viz:
  A[] array = new A[10];
  array[0] = new SubclassOfA();


I'd also suggest, when you ask questions, that you provide real names for your classes. It's much easier for us to work out what's going on if you provide names like Animal and Dog to illustrate your point, rather than A and B, etc.

How can I print this info if I can not access the information of the subclass as name, employee salary?


Follow Jeanne's advice.

HIH

Winston
+Pie Number of slices to send: Send
 

Jeanne Boyarsky wrote:Can the subclass provide a toString() method? Or getter?



MMM... In designing is not expected, but I think so.

Winston Gutkowski wrote:
I don't understand that statement, but an object of class A cannot be assigned to an array that is defined as being a subclass of A. ie, you can't write:
SubclassOfA[] array = new SubclassOfA[10];
array[0] = new A();
You can, of course, do the opposite though, viz:
A[] array = new A[10];
array[0] = new SubclassOfA();



Okok.. then.. Class Company, which has an array object of Employees, in designing these two classes are related by aggregation (UML).
Then, There are two subclasses of Employees: Grapghic and Programmer.

With override of toString(), is not possible, why would I do that in class Company.

In the array employees of type Employees, for example, I have 3 employees:
1. Employees
2. Graphic
3. Programmer

In class Company, through the method employeesList, I have to print the information of all employees in the array, such as name, lastname, salary ecc...
+Pie Number of slices to send: Send
 

manu lix wrote:Okok.. then.. Class Company, which has an array object of Employees, in designing these two classes are related by aggregation (UML).
Then, There are two subclasses of Employees: Grapghic and Programmer.
With override of toString(), is not possible, why would I do that in class Company.


OK, now we're getting somewhere. You didn't explain any of that in your first post, which was why Jeanne suggested what she did - and what she said still makes sense (see below).

This is why it's VERY important to TellTheDetails (←click) when you're asking a question.

BTW, that class should be called Employee (singular); not Employees. I notice that you haven't used a plural for either of your subclasses.

In class Company, through the method employeesList, I have to print the information of all employees in the array, such as name, lastname, salary ecc...


OK, so override toString(), starting with Employee (unless it's simply an interface). However:

1. You need to be careful about that, because this "list" actually looks like a proper report, and toString() is mainly used to provide a "readable" String for debugging; it's not really meant for "formatted" output.
On the other hand, if you have a reasonably readable toString() for each Employee, you can print them all out directly with:
  System.out.println( Arrays.toString(employees) );

2. If I see a method called employeesList(), I would expect it to return a List<Employee> (ie, a java.util.List), not a String (or void).

HIH

Winston
+Pie Number of slices to send: Send
 

In class Company, through the method employeesList, I have to print the information of all employees in the array, such as name, lastname, salary ecc...
OK, so override toString(), starting with Employee (unless it's simply an interface). However:

1. You need to be careful about that, because this "list" actually looks like a proper report, and toString() is mainly used to provide a "readable" String for debugging; it's not really meant for "formatted" output.
On the other hand, if you have a reasonably readable toString() for each Employee, you can print them all out directly with:
  System.out.println( Arrays.toString(employees) );

2. If I see a method called employeesList(), I would expect it to return a List<Employee> (ie, a java.util.List), not a String (or void).

Winston



1. In fact, that 's what I tried to do: System.out.println( Arrays.toString(employees).
Evidently, I do not have implemented well toString() in class Employee.
Take a look:


I am not clear that by overriding the method toString(), it can be used in another class, in this case in class Company
Howewer, on the point 2, let's ignore for the moment.

Because I have great difficulty in understanding the printing and manipulating data in arrays.. In fact, also in other programs have similar problems.

Going back to method toString(), I get all the elements of Arrays! Also those null.
+Pie Number of slices to send: Send
Ok, so you have a list of Employees.
An Employee can be specialised as a Graphic Designer, or a Programmer, or presumably they can just be a regular employee.

>Method listEmployees should print Employee information
>Q: How can I print this info if I can not access the information of the subclass as name, employee salary?

A: you can't print the info if you don't have access to it. So you either
- only deal with the data that you ARE aware of.
- delegate to something that DOES have access (as suggested already by using the toString method)

You said that you wanted to list all employee names, and their salaries.
Where do these properties live? Do not all Employees have them, and so you don't even need to care about the subclasses?
+Pie Number of slices to send: Send
 

Ok, so you have a list of Employees.
An Employee can be specialised as a Graphic Designer, or a Programmer, or presumably they can just be a regular employee.


Yes.

Stefan Evans wrote:You said that you wanted to list all employee names, and their salaries.
Where do these properties live? Do not all Employees have them, and so you don't even need to care about the subclasses?



Actually I was merely experimenting this info from main:


The method addEmployee takes care of adding the Employee in the array.
1
+Pie Number of slices to send: Send
 

manu lix wrote:I am not clear that by overriding the method toString(), it can be used in another class, in this case in class Company


Sure you can. toString() is a public method, so:
1. Anything can call it.
2. It can't be overridden by a method that is anything less than public.

Because I have great difficulty in understanding the printing and manipulating data in arrays.. In fact, also in other programs have similar problems...


I believe I already suggested one way. In your Company class (and assuming your Employee array is called 'employees'):It probably won't be exactly what you want, but at least it'll prove that implementing toString() allows you to print out the contents of your array.

You can always "pretty up" the output later. The main thing right now is to get it working.

Going back to method toString(), I get all the elements of Arrays! Also those null.


Well that's a completely different problem. Plainly you're not setting them correctly. Why don't you have another look at your add() method, because that's almost certainly where THAT problem lies.

But basically, you've written far too much code without compiling and/or testing it to make sure it works.
Consequence: You're now having to solve SEVERAL problems at once.

HIH

Winston
+Pie Number of slices to send: Send
I solved, I solved it, but I have not used Arrays.toString().
Howewer I undertstood, thanks
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 644 times.
Similar Threads
Java Array Help
How to print values in pre defined reciept
Java 3D Programming by Daniel Selman
EJB3 org.hibernate.LazyInitializationException:failed to lazily initialize a collection of role
no persistent classes found for query class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:10:52.