I want to use Inheritance in a static way

I have a bunch of subclasses extending a Skill Type,

public abstract class Skill {

	// current level
	private static int fishLevel = 1;
	private static boolean isFishing = false;

	private static int miningLevel = 1;
	private static boolean isMining = false;

	private static int attackLevel = 1;
	private static int defLevel = 1;
	private static int strLevel = 1;
	private static int teleLevel = 1;

	public Skill(String name) {
	}

	/**
	 * @return the fishLevel
	 */
	public static int getFishLevel() {
		return fishLevel;
	}

	/**
	 * @param fishLevel
	 *            the fishLevel to set
	 */
	public static void levelUpFishing() {
		fishLevel++;
	}

	/**
	 * @return the isFishing
	 */
	public static boolean isFishing() {
		return isFishing;
	}

	/**
	 * @param isFishing
	 *            the isFishing to set
	 */
	public static void setIsFishing(boolean isFishing) {
		Skill.isFishing = isFishing;
	}}

I want to use Inheritance so i dont have to keep typing the same stuff again and again, like i have been doing :



/**
 * Uses the static methods
 * 
 * @author Erick
 *
 */

public class Fishing extends Skill {

	public Fishing() {
	}

	public static void levelUp() {
		levelUpFishing();
	}
	
	public static String getId() {
		return "Fishing";
	}

}


I know I could use the static methods in the Skill Class, but i also need to set Icons and other things, that inheritance would help out with tremendously, is there a way to do this, I don’t want to make instances of each skill.

If you want to do things like static inheritance you probably should re-think your OO-design.

Why does the class Skill has fields specific to “Fishing” although you have an explicit class for it as well?
Why not instantiate skills? I could imagine different characters having a different skill-level makeing and OO-approach a necessity.

thats a good question, this was a example lol

I dont want to instantiate each skill because they simply don’t need to be, I am just using it store the current level and name and other things, so the when one levels up fishing, i can simply Fishing.levelUp(); , and later in the game if i need to get the level i can just Fishing.getLevel(); , but i need to do this with all of the levels in a static way, so i cant make a interface, or abstract methods, just wondering if there was a way to do it.

BTW it is a game like runescape, so the skills are changing alot. not all entities has the same skills.

I’d create a MetaSkill class and put the static stuff in there as non-static. Create an instance of MetaSkill and use it instead of Skill for the formerly static methods. MetaSkill can inherit from another class and implement interfaces. It can also be inherited from. Your Skill subclasses can all work with one MetaSkill instance, or you can create several MetaSkill instances with different data. You could even have your different Skill subclasses reference different MetaSkill subclasses. You might even find it handy to have MetaSkill itself be an interface. (You’ll probably find it handy to have a Skill method that returns the relevant MetaSkill instance.)

Anyway, you get the idea. Do what works for you. Trying to make statics work here just doesn’t work; I’ve tried it.

Awesome, thanks for answering in a programmer fashion :slight_smile: I didn’t think of this.

Well im spending to much time on this, so I’m going to just instantiate everything…lol

package com.supafool.apps.games.supascape.skill;

/**
 * Abstract class used to hold data for each skill
 * 
 * @author Erick
 * 
 */

public abstract class SkillAb {

	// current amount of xp
	protected double currentXp = 0.00;

	// xp needed to get next level
	protected double levelXp = 150.00;

	// Current level, not max level
	protected int currentLevel = 1;

	// Max Level is the max you can have, like 8/10, this is what levels up
	protected int maxLevel = 1;

	// Amount to raise the levelXp after skill has been leveled
	private final double levelOffSet = 1.35;

	// used to keep 2 skills from being called at the same time
	protected boolean isSkilling = false;

	public SkillAb() {
		initSkills();
	}

	public abstract void initSkills();

	public void gainXp(double amountToGain) {
		currentXp += amountToGain;

		// check is needs leveled
		if (currentXp > levelXp) {
			levelUp();

			// raise xp needed to level again
			levelXp = levelXp * (maxLevel * levelOffSet);

		}

	}

	public int getCurrentLevel() {
		return currentLevel;
	}

	public void levelUp() {
		maxLevel++;

		// Fill up current Level
		currentLevel = maxLevel;
	}

	public void setIsSkilling(boolean b) {
		isSkilling = b;
	}

	public boolean isSkilling() {
		// TODO Auto-generated method stub
		return isSkilling;
	}

	public abstract String getSkillName();

}

It is better to put this information at instance level. Storing a lot of state in static variables is asking for trouble. Static variables should be for unchanging (or almost unchanging) values or stateless utility functions. If you make your skills static you also force yourself into having only one instance of the skill - what if want to give the same skill to monster, or if the player can have multiple characters, or if you dare to try add multiplayer…?

Just get something running. You can junk the statics later when (A) you can see that you have to and (B) you’ve got a better idea how to junk them. Doing a lot of work that doesn’t immediately get your game going is a drag. Doing a lot of work that, in the end, doesn’t do what you want is typical big business IT, but not something you want to do when you’re not middle management.

There’s a lot to be said for doing it right, but don’t get ahead of yourself.

Actually understanding what you’re doing and how things like OOP works is better than “just getting something running”. There’s inelegant code, and some code’s always going to feel that way, and there’s totally missing basic concepts of design. The first will just annoy you, the latter will ultimately keep you from getting what you want done.

But if it’s your first few projects … just get something running :slight_smile:

I love an oxymoron as much as I love giraffes and this threads title is no exception.