Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6805557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:36:04+00:00 2026-05-26T19:36:04+00:00

I’m currently working on some school project; we are developing a simple RPG, but

  • 0

I’m currently working on some school project; we are developing a simple RPG, but I seem to be questioning some of my Java code.

I have an Abstract class called Item, then I have Armor and Weapon classes which both inherit from Item. Both Armor and Weapon have some attributes that Item doesn’t have.

So in my database wrapper, I have a loaditem method, which gets the stats and creates an instance of my class (read comments in code):

public Item loadItem(int itemID)
{
    // Setting item, as a Item, as it can be both Weapon or Armor 
    Item item;

    try {

        // Build prepared statement
        PreparedStatement stmt = con.prepareStatement("SELECT * FROM item WHERE iid = ?");

        // Set Parameter
        stmt.setInt(1, itemID);

        // Execute the query
        ResultSet rs = stmt.executeQuery();

        // Get row
        rs.first();

        // Set all values and keys


        if(rs.getString("type").equals("weapon")) { 
            item = new Weapon();
            // SetDamage is unique to Weapon, and setDefense to armor  
            item.setDamage();
        }else {
            item = new Armor();
            item.setDefense()
        }

        item.setIid(rs.getInt("iid"));
        item.setName(rs.getString("name"));
        item.setDescription(rs.getString("description"));
        item.setSellValue(rs.getInt("sellvalue"));
        item.setBuyValue(rs.getInt("buyvalue"));
        item.setStrength(rs.getInt("strength"));
        item.setAgility(rs.getInt("agility"));
        item.setEndurance(rs.getInt("endurance"));

I have no problem setting a instance of Weapon or Armor to item, but it can’t find the methods in the classes.

Do I really need to create both a weapon and an armor object, and return after type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:36:04+00:00Added an answer on May 26, 2026 at 7:36 pm

    Yes, if you’re going to call an instance method, the “target” expression of the method call must have a compile-time type which is known to have the appropriate method. The compiler needs to resolve the method calls at compile time. (Any overload resolution is done at compile-time; the actual implementation is decided at execution time based on polymorphism of course.)

    It’s unclear why you’d want the item variable in the first place. It sounds like you should just have:

    if (rs.getString("type").equals("weapon")) { 
        Weapon weapon = new Weapon();
        weapon.setDamage();
        return weapon;
    } else {
        Armor armor = new Armor();
        armor.setDefense();
        return armor;
    }
    

    That’s assuming you want to return immediately – your code was truncated, so it’s not clear. If you really do need the item variable, you can just assign weapon or armor to item where I have the return statements above.

    EDIT: It sounds like that’s the case, so you’d have:

    // I'd still declare `item` as late as possible, i.e. here rather than
    // outside the `try` block, unless you *also* need it outside the try block...
    Item item;
    if (rs.getString("type").equals("weapon")) { 
        Weapon weapon = new Weapon();
        weapon.setDamage();
        item = weapon;
    } else {
        Armor armor = new Armor();
        armor.setDefense();
        item = armor;
    }
    // Other code using `item` here
    

    In fact, you may well want to put the code to initialize the weapon into a separate method. Then you could use:

    String type = rs.getString("type");
    Item item = type.equals("weapon")) ? createWeapon() : createArmor();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.