This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem With Method and Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem With Method and Interface" Watch "Problem With Method and Interface" New topic
Author

Problem With Method and Interface

Scott Kamen
Greenhorn

Joined: Apr 01, 2003
Posts: 15
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

Joined: Jan 30, 2002
Posts: 3451
So Scott, you didn't actually say what your problem was. What exactly do you need help with?


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Scott Kamen
Greenhorn

Joined: Apr 01, 2003
Posts: 15
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

Joined: Jan 30, 2002
Posts: 3451
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

Joined: Apr 01, 2003
Posts: 15
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

Joined: Apr 15, 2002
Posts: 78
Michael is correct. Try this:

[ May 04, 2003: Message edited by: Irene Loos ]

Irene Loos
Scott Kamen
Greenhorn

Joined: Apr 01, 2003
Posts: 15
I tried:

and

and I still get the same compile error.
thanks again for the help,
-Scott
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Scott Kamen
Greenhorn

Joined: Apr 01, 2003
Posts: 15

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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Scott Kamen
Greenhorn

Joined: Apr 01, 2003
Posts: 15
Wow. I'm an idiot. I didn't see that.
thanks a ton,
-Scott
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem With Method and Interface
 
Similar Threads
Problem With Very Simple While Loop
I cannot fix this program! if anyone can help!
Problem with abstract methods
Super class
question about adding to a vector