Results 1 to 7 of 7
  1. #1
    indyjoel is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default Class, SubClass type (InstanceOf) Question

    Hi

    am totally confused an am looking for a direction.

    My professor was NOT impressed with the use of instanceof but I cannot see (and I have researched and searched this) another way, even though I know it exists.

    Apologies but I am just starting my Java journey and this has confused the you know what out of me

    Here is what I have done so far

    The area where I check if the productline (p) is an instance of PerishableProductLine is where I would like to use a different method of determining whether p is a PerishableProductLine or just a Product.

    Java Code:
    Application.java
    
    private static void endOfDay()
        {
            System.out.print("Decrement days to expiry [Y/N] ?");
            String decrementDays = getStringInput();
            List products = Inventory.getProducts();
            if ("Y".equalsIgnoreCase(decrementDays))
            {
                for (int i = 0; i < products.size(); i++)
                {
                    
                    ProductLine p = (ProductLine) products.get(i);
                    
                    if (p instanceof PerishableProductLine)
                    {
                      int daysToExpire = ((PerishableProductLine) p).getDaysToExpiry();
                      if (daysToExpire > 0)
                      {
                          ((PerishableProductLine) p).setDaysToExpiry(daysToExpire - 1);
                      }
                    }
                }
    }
    
    Inventory.java
    
    public static ArrayList getProducts()
        {
            return products;
        }
    
    
    
    ProductLine.java
    public class ProductLine
    {
    
    public ProductLine(String pc, double rrp, double sp,int oh, String desc){
            productCode = pc;
            recRetPrice = rrp;
            salePrice   = sp;
            onHand      = oh;
            soldToday   = 0;
            description = desc;
        }
    
    
    PerishableProductLine.Java
    public class PerishableProductLine extends ProductLine
    {
    
    
        public PerishableProductLine(String pc, double rrp, double sp,int oh, String desc, int expiryday){
            super(pc, rrp, sp,oh, desc);
            setDaysToExpiry(expiryday);
        }
    
    }
    I have been reading that I need to overload the methods, but I just can't see it

    Thanks Again
    Attached Files Attached Files

  2. #2
    Honopac is offline Member
    Join Date
    Oct 2010
    Location
    Fort Wayne, IN
    Posts
    7
    Rep Power
    0

    Default

    PerishableProductLine is-a productLine (inheritance relationship), but not the opposite. Inheritance does not work in reverse. Means, you cannot check whether productLine is an instance of perishableProductLine because it is not. You can also use getClass() to check this type of relationship.

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Just looking at it, what you did seems alright. But if he's stressing method overloading, I can think of 2 ways of handling it.

    The first is the most easiest(though I'm not sure it's really what your professor wants). Create a method inside Product called isPerishable() which returns a boolean(In this case false). Then override it inside the PerishableProduct to return true. This is basically a work around way of doing instanceof.

    The second would be to move the handling of the end of day processing to the actual products. Create a method inside Product called endOfDay() which handles the end of day processing for a basic product. Then override that method inside PerishableProduct and add the special handling for PerishableProduct's end of day.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Quote Originally Posted by StormyWaters View Post
    Just looking at it, what you did seems alright. But if he's stressing method overloading, I can think of 2 ways of handling it.

    The first is the most easiest(though I'm not sure it's really what your professor wants). Create a method inside Product called isPerishable() which returns a boolean(In this case false). Then override it inside the PerishableProduct to return true. This is basically a work around way of doing instanceof.
    Not this. ProductLine should not care what things that extend it can do, otherwise you'd find yourself adding flags for canFloat(), canSing(), canTapDance().

    Quote Originally Posted by StormyWaters View Post
    The second would be to move the handling of the end of day processing to the actual products. Create a method inside Product called endOfDay() which handles the end of day processing for a basic product. Then override that method inside PerishableProduct and add the special handling for PerishableProduct's end of day.
    This.

  5. #5
    indyjoel is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks Tolls

    advise was spot on

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Various
    Create a method inside Product called endOfDay() which handles the end of day processing for a basic product. Then override that method inside PerishableProduct

    endOfDay() == doSomething() in what I posted in the other thread.

  7. #7
    indyjoel is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    endOfDay() == doSomething() in what I posted in the other thread.

    this is the code I put in place

    ProductLine p = (ProductLine) products.get(i);
    if (p.eod() == "IPL")
    {
    IPL.add(p);
    System.out.println(p);
    }
    else
    System.out.println(p);

    }

    Then inside productline.java
    public String eod()
    {
    StringBuilder result = new StringBuilder();
    String NEW_LINE = System.getProperty("line.separator");
    result.append("Perishable ProductLine" + NEW_LINE);
    //return result.toString();
    return "PL";
    }

    and overrode it in PerishableProductLine.java
    public String eod()
    {
    StringBuilder result = new StringBuilder();
    String NEW_LINE = System.getProperty("line.separator");
    result.append("Perishable ProductLine" + NEW_LINE);

    if (getDaysToExpiry() > 0)
    {
    setDaysToExpiry(getDaysToExpiry() - 1);
    }

    return "IPL";
    }

Similar Threads

  1. Super class refering to subclass
    By sujith123 in forum New To Java
    Replies: 3
    Last Post: 10-05-2009, 09:00 AM
  2. super instanceof Class?
    By mikeiz404 in forum New To Java
    Replies: 11
    Last Post: 01-23-2009, 07:23 PM
  3. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  4. subclass vs inner class
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:31 PM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 PM

Posting Permissions

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