• Post Reply Bookmark Topic Watch Topic
  • New Topic

basic question on print object array  RSS feed

 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can the subclass provide a toString() method? Or getter?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved, I solved it, but I have not used Arrays.toString().
Howewer I undertstood, thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!