Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem With Method and Interface

 
Scott Kamen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a project that has been going well but I have one snag that I can't figure out. I have an application that has an App class , a Person class , an Employee class that extends Person , a Customer class that extends Person and implements Billable , and a Billable interface. I have created a vector that contains Person objects in which some are Customer objects and others are Employee objects. I'm trying to get at the amountDue of these objects as you can see in the code below but I'm not sure if the problem is somewhere else in the code (possibly in the other classes) so I will include all of the code in my project.

The code for the entire application is here:

If someone could give me some insight on this it would be great.
If you're still reading this long post I'm eternally greatful and thankful.
thanks,
-Scott
Edited to try to stop horizontal scrolling - barry
[ May 04, 2003: Message edited by: Barry Gaunt ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Scott, you didn't actually say what your problem was. What exactly do you need help with?
 
Scott Kamen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting a compile error when I try to using my amountDue method. As I wrote in my comment when I try to use the amountDue method of the objects in the loop I get a "cannot resolve symbol : method getAmount ()" . It should be able to work because if they pass through the if statement then they must be Customer objects. I also tried using an instanceof statement to test this. I know it must be hard for you guys to understand what I'm getting at it's just kind of hard to explain because I'm pretty sure my problem has to do with one of my custom classes.
thanks for the help,
-Scott
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Person consumer = (Customer) people.get(i);
Bit by polymorphism! If I read your first post correctly, the Customer is a Billable, but Person is not. So casting to the more general class will cause a compile time error because Person does not have a getAmount() method. You have the right idea, it's just that you should cast to the interface instead (or Customer). Try this instead:

On editing my post I noticed that you had System.out.prinln instead of System.out.println. Maybe that was the problem?
[ May 03, 2003: Message edited by: Michael Morris ]
 
Scott Kamen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah the System.out.println was a typo I made at the last second and not the root of the problem. I took your advice and change the Billable stuff but I still get the same compile error that I did before. Quite strange.
thanks for the help though,
-Scott
 
Irene Loos
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael is correct. Try this:

[ May 04, 2003: Message edited by: Irene Loos ]
 
Scott Kamen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried:

and

and I still get the same compile error.
thanks again for the help,
-Scott
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've scanned through this post and would like to help if I can. However, I don't have the time to go through every line of code that you posted. Will you please post the line that gives the compiler error (and a few lines before and after to give it some context), along with the exact compiler error. This should help clear things up so that we can point out the exact changes you need to make to fix the problem.
Cheers!
Layne
 
Scott Kamen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

After the suggestion from Michael and Irene I changed the the one statement to Customer consumer = (Customer) people.get(i) which is the line before where I still get the same compile error that I did originally. The compile error is: cannot resolve symbol : method getAmount () .
thanks again for the help,
-Scott
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what class is method getAmount() declared? Should it not be amountDue() ?
That is: System.out.println( consumer.amountDue() );
[ May 05, 2003: Message edited by: Barry Gaunt ]
 
Scott Kamen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. I'm an idiot. I didn't see that.
thanks a ton,
-Scott
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic