Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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!

  • #2
    New Coder
    Join Date
    Mar 2011
    Location
    Utah
    Posts
    30
    Thanks
    0
    Thanked 6 Times in 6 Posts
    -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.

  • Users who have thanked mimis40 for this post:

    hypertone (04-15-2011)

  • #3
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Oh wow, I didn't know I could EXTEND and IMPLEMENT at the same time. TY for your help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •