• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a method from a subclass to the Object class?  RSS feed

 
Emma Penny
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am working on a project and I've gotten it to work about 50%. I'm stuck on the other half. So i've created a project with a main class: EmployeeReport that is supposed to print employee information, a class titled Employee where I've written the general data about employees, and two subclasses of Employee titled Secretary and Supervisor, where I've written additional data about employees that are secretaries and supervisors, respectively.
Currently, my employeereport class is designed to work only with the interface provided by the employee class. For the project, I have to Create an array of employees which contains
Employee (3), Secretary (4), and Supervisor (4) and print the information for regular employees without including the secretary and supervisor information, and print the information for secretaries and for supervisors.
I'm thinking I have to have a method that converts the objects to strings in the superclass and redfine it in the subclasses to add the extra information and then call that method in the employeereport class. I've attempted to do that, but I've gone and confused myself on how to properly use toString and how to call it when needed.

Sorry if this is long.
I really appreciate any help, as this has been bothering me for 2 days!

Here is my code

 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome tho the Ranch!

You can use super.toString() to call the toString method of the superclass. You can append values of any new fields to the result.


By the way, your equals methods can throw NullPointerException if the fields happen to be null. Object.equals is not allowed to throw any exceptions.

Use Objects.equal instead.Or make sure your fields are never null.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I don't like the comments you wrote. Let's discuss a little bit about them.

1.

Since you discuss implementation details in your comment, what extra information you provide in it? From the statement I know already what you are doing there. So please answer, why I supposed to read comment?
Probably others would say, that comment isn't even correct as it won't hold employee instances, but rather reference variables of type Employee, which may refer to Employee instance/-s.

2.

Again. "putting object into correct slot...", who defines correctness of the slot? "i" ? The point I'm making, that you write some words which give little to nothing benefit.

3.

I won't say that comment is useless, you know that by now already. Let's look what we can do better about it. Look how code below is neater. Probably there could be found better method name, but that is a start.

Always try to express the program's idea with clear variables, method names. Same applies to class's names.

Then I see you are having hard times coming up with variable names within constructors. I have in mind "tl", which probably means title. Do you know "this" keyword? Google it and use it. Keep "title" variable name, not "tl".
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More...


1. Supervisor class. So, what we see here, are 2 instance variables. Comment says it is a data field. Why not fields if two of them? Or which one is data field out of those two?

2. Make them private.

3. empCount - I'd say it isn't a good variable name. What it represents? Why you allow set this count to -4321 ? Is it a valid count? How you ensure you won't have duplicates? Are they allowed or not?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I am afraid there is another error in the equals() method; it is necessary to override equals() but you have overloaded it by giving it a different parameter type. It shou‍ld take Object as its parameter.Line 4: test whether you have the same object twice; if so there is no need for further testing: you know it will be equal.
Line 5: You only go into the if when you haven't already shown equality. You must show the two are compatible types before going on to the cast in line 6. Challenge, if you are feeling ambitious:- Find out which part of line 5 you can omit and the code will still work correctly. [Additional]: If you pass null, you won't enter the if, so your method will return false.
Line 6: Since null is a permissible value for some fields, you need to write a test which can cope with that. Objects.equals makes that test much easier, which is why Paweł suggested it. More details here. Note that is Objects not Object.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed that there is wrong parameter type.

This error could have been avoided by using the @Override annotation.

And one more important thing. When you override equals you have to override hashCode as well.
 
Emma Penny
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys!
Thank you guys for all your help, I ended up asking my professor for his opinion and he helped me out before I read the replies to this.
I still really appreciate you guys taking your time to help me out and improve my code! I will definitely be back with more questions.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure

How much did his opinion differ from ours ?
 
Emma Penny
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What he told me was pretty much the same thing, minus the comments issue (though I did fix them after I saw the post), and additional information on some of my methods in my subclasses.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!