Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-14-2011, 04:13 AM   PM User | #1
hypertone
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
hypertone is an unknown quantity at this point
Polymorphism and Inheritance

Hi guys,

Modify the Firm example from this chapter such that it accomplishes its polymorphism using an interface called Payable. Given the class hierarchy:

Firm - Staff -- StaffMember +--- Employee +---- Executive
| +---- Hourly
| +--- Volunteer

Declare "interface Payable ..." in a seperate file that contains pay(). Now you have to implement this interface before you can use pay().


So I have to implement the interface Payable before I can use pay(), BUT how can I do this when each class has a different body for pay()? Also, would StaffMember been implementing Payable or would it be Employee?

Thanks for any help guys!
hypertone is offline   Reply With Quote
Old 04-14-2011, 07:50 AM   PM User | #2
mimis40
New Coder

 
Join Date: Mar 2011
Location: Utah
Posts: 30
Thanks: 0
Thanked 6 Times in 6 Posts
mimis40 is on a distinguished road
-For the pay() question it's easy, all you have to do is override it.
-As for the StaffMember question, classes don't implement each other, they extend each other. But according to your hierarchy, if I'm reading this right, Employee should extend StaffMember and Executive should extend StaffMember like so:

Code:
public class Firm{
    //whatever code here
}
public class Staff extends Firm{
    //whatever code here
}
public class StaffMember extends Staff{
    //whatever code here
    public boolean Payable(){
        return true;
    }
}
public class Employee extends StaffMember implements Payable{
    //whatever code here
    //override what is originally in the StaffMember class
    public boolean Payable(){
        //return false because they are a volunteer
        return false;
    }
}
public class Executive extends StaffMember implements Payable{
    //whatever code here
    //no need to override payable because executives are payed
}
I may be wrong on what I inferred what you wanted exactly what you wanted from your question but i hope this at least gets you started.

Last edited by mimis40; 04-14-2011 at 07:55 AM..
mimis40 is offline   Reply With Quote
Users who have thanked mimis40 for this post:
hypertone (04-15-2011)
Old 04-14-2011, 04:27 PM   PM User | #3
hypertone
New to the CF scene

 
Join Date: Feb 2011
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
hypertone is an unknown quantity at this point
Oh wow, I didn't know I could EXTEND and IMPLEMENT at the same time. TY for your help!
hypertone is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:45 PM.


Advertisement
Log in to turn off these ads.