Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Method takes parent class as parameter, realized I can't enter it's subclass as a parameter

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Method takes parent class as parameter, realized I can't enter it's subclass as a parameter

    Ok so I'm working on making a simple text-based game, and I've got two methods - one that removes an item from the player's inventory, and another that works with breaking said item. The issue comes in in that any object of class Items can be removed from inventory via the method removeFromInventory. The method breakItem only works for CombatItems, however, which are a subclass of Items. Only they can break, as only they have durability. Im not sure what it is that I can do, but heres the code from both methods, if its any help in figuring out what to do.


        public void removeFromInventory(Items I)
        {
            if(Inventory.size() >= 0 && Inventory.indexOf(c) >= 0)
            {
                Inventory.remove(I);
            }
     
            else
            {
                System.out.println("That item isn't located in your inventory.");
            }
        }
     
        public void breakItem(CombatItem c)
        {
            if(c.getCurDurability() <= 0)
            {
                removeFromInventory(c);
            }
        }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    On Vacation
    Posts
    21,995
    Thanks
    52
    Thanked 2,364 Times in 2,318 Posts

    Default Re: Method takes parent class as parameter, realized I can't enter it's subclass as a parameter

    Im not sure what it is that I can do
    Can you explain what you are trying to do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Method takes parent class as parameter, realized I can't enter it's subclass as a parameter

    I want to put CombatItems in as a parameter of the removeFromInventory method, but that only takes in Items as a parameter, which CombatItems are a subclass of.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    On Vacation
    Posts
    21,995
    Thanks
    52
    Thanked 2,364 Times in 2,318 Posts

    Default Re: Method takes parent class as parameter, realized I can't enter it's subclass as a parameter

    I want to put CombatItems in as a parameter of the removeFromInventory method
    What happens when you tried coding that and compiling it? Were there any error messages?
    Copy the full text of any error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    moosyman (February 9th, 2019)

  6. #5
    Junior Member
    Join Date
    Feb 2019
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Method takes parent class as parameter, realized I can't enter it's subclass as a parameter

    Quote Originally Posted by Norm View Post
    What happens when you tried coding that and compiling it? Were there any error messages?
    Copy the full text of any error messages and paste it here.
    Okay so, I just realized what I could've done and avoided all this trouble, which was just overloading the method. In any case, the error I got was "Incompatible types: CombatItem cannot be converted to Items"

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    On Vacation
    Posts
    21,995
    Thanks
    52
    Thanked 2,364 Times in 2,318 Posts

    Default Re: Method takes parent class as parameter, realized I can't enter it's subclass as a parameter

    the error I got was "Incompatible types: CombatItem cannot be converted to Items"
    Should that be: CombatItems?
    That's confusing because you said:
    CombatItems, however, which are a subclass of Items
    Are there two separate classes with almost the same name, one with an ending s?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    244
    My Mood
    Cool
    Thanks
    0
    Thanked 32 Times in 30 Posts

    Default Re: Method takes parent class as parameter, realized I can't enter it's subclass as a parameter

    Not certain how this compiled unless c is an instance field.

    public void removeFromInventory(Items I)
        {
            if(Inventory.size() >= 0 && Inventory.indexOf(c) >= 0)
            {
                Inventory.remove(I);
            }
     
            else
            {
                System.out.println("That item isn't located in your inventory.");
            }
        }

    You're checking for c but removing I.

    Regards,
    Jim

  9. The Following User Says Thank You to jim829 For This Useful Post:

    moosyman (February 9th, 2019)

Similar Threads

  1. [SOLVED] Parameterized array type as a method parameter
    By Lediator in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 1st, 2014, 01:08 AM
  2. Method calculate 2 object using constructor with parameter
    By DavidXCode in forum Object Oriented Programming
    Replies: 2
    Last Post: November 21st, 2012, 08:31 PM
  3. Replies: 3
    Last Post: May 27th, 2012, 11:11 AM
  4. Replies: 1
    Last Post: April 14th, 2012, 01:45 PM
  5. how can i pass objects into a parameter and display them in the method
    By em1980 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 5th, 2011, 12:02 PM

Tags for this Thread