• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not getting the output expected  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run my program and choose option 2 to get contact details I am expecting it to display as follows (First Name, Last Name):

Contacts how have been entered:
0) John Doe
1) George Smith
2) Nancy Davis
Please enter the number corresponding to the contact you would like to view:

Instead for a personal contact it is displaying as follows:
Contacts who have been entered:
0) Doe 1 F St. (last name, address)
Please enter the number corresponding to the contact you would like to view:

Instead for a business contact it is displaying as follows:
Contacts who have been entered:
0) 1 F St. jd@gmail.com (address, email)
Please enter the number corresponding to the contact you would like to view:

Then when I enter the number to display the contact for personal it is returning me only first name and business is only returning me first and last name. It should be returning the full contact info that was put in during the add contact step. I thought I programmed everything properly but it isn't displaying what I want to see. Any help would be greatly appreciated. My code is listed below.

ContactList



Contact



Personal



Business

 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unlike constructors, methods do not have an implicit call to the superclass method that was overriden. You have to explicitly call them with super.whatever(). In this case, you have to call super.toString() and use the returned value along with whatever else you want to tack on to it. If you don't call super.toString, then only what you wrote is what you're going to get.

BTW, I'm not implying that constructors are overridden, because they're not. The point is that you have to explicitly call the overridden super method if you want to execute the code there, otherwise, only the overriding code gets executed.
 
Tim Davis
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I am missing something but you are saying in my Personal and Business subclasses I need to add to my return statement super.toStirng() along with any additional info I would like to see?
 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim ,
I am curious to know few things on your code :
1) Why your have defined the class Contact as abstract , (still there is no abstract method !!!)
2)Why you override the toString() of Business and Personal like this :




why didnt you take other inherited properties here ? like firstName , lastName etc ..

and finally the last question :
3)why you have defined all properties (in Contact ,Personal and Business) as String ? Probably this is the most important question .Why am I asking this because you can find the solution of your problem in here.
You have done silly mistakes of calling constructor in Personal and Business class .It is showing you are passing different values from ContactList class to the respective constructor. Change constructor of Personal and Business accordingly , you can get the desire result .

Hope you get the point ..
Satya
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Majumder wrote:
You have done silly mistakes of calling constructor in Personal and Business class .It is showing you are passing different values from ContactList class to the respective constructor. Change constructor of Personal and Business accordingly , you can get the desire result

To clarify this, check the parameters you are passing to your constructors; Java doesn't care about the names, it only cares about the positions and types. That is, the first value passed will be assigned to the first parameter declared and so on. The names are for your benefit, so you know what the value you are passing means or what it's for.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!