abstractMethodError

OK so…

the attack() method is inside the Monster class

class Attack is an abstract class. It has MANY subclasses( so far about 10, planned to exponentiate ). The idea is, that attacks cause damage( or don’t ), and also have some other effect in the battle such as raising its own attack power or lowering an enemy’s defense power. So, class Attack has an abstract method called runEffect( ), which each subclass is forced to override.

For example,


Monster m = new Monster( );
m.attack( new SwordSlash( ), badGuy );

SwordSlash not only does damage, but with a 50% chance it can also lower the target’s defense. So its runEffect() method runs the math, if the Monster is lucky, he lowers his target’s defense.

public void attack( Attack a, Monster target )
	{
		if( a == null )
			return;

		System.out.println( "\n" + this.name + " used " + a.getName( ) + "!" );

		int damage = 0;
		if( a.getPower( ) != 0 )
			damage = calcDamage( a, target );

		target.takeDamage( damage );

		a.runEffect( target ); //AbstractMethodError
		a.decrementUsesLeft( );
		this.setAttackChoice( null );
	}

I understand the error. SwordSlash is being demoted to just an Attack, and a.runEffect( ) is trying to use the runEffect( ) method in Attack which is abstract, hence the error. The question is what now? I don’t want to write a different attack( ) method for each subclass of Attack.

I have never seen that error before in my entire Java life. And I thought I’ve seen them all.
Could you care to print the entire stack trace?
Plus is this a runtime or compiletime error?

EDIT: I understand the concept behind what is going on… if the method were

Monster.attack( SwordSlash, Monster )

then this would work fine. The idea I’m missing is why even bother using an Attack parameter if I can’t use its subclasses methods.

There isn’t much to the stack trace…

It’s a runtime error( sorry, I should have mentioned that. In my head it was a compiler error ).
The runtime error occurs when the program comes across nonstatic Monster.attack( Attack, Monster ).

The Battle class essentially accepts two Monsters and loops them through a series of attacks until one’s health is 0.
The battle loop is currently in the constructor of the Battle class… bad, I know.

[quote]Exception in thread “main” java.lang.AbstractMethodError: Attack.runEffect(LMonster;)V
at Monster.attack(Monster.java:194)
at Battle.(Battle.java:62)
at Battle.main(Battle.java:127)
[/quote]

That is supposed to work fine with polyphormism and all.
Try recompiling all your java files and re-running it.
If you still get the error, could you paste the code for your Action, SwordSlash and Monster?

Java supports letting you write attack(Attack a) and passing in a SwordSlash and having it run the correct runEffect() method. Normally the compiler should catch things like that. If you look at the API for the exception: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/AbstractMethodError.html it sounds like you have a problem with an out-of-date .class file in your application. If you’re using Eclipse try doing a clean build, if you’re compiling manually with javac make sure you recompile both Attack, SwordSlash and Monster.

Gah ra4king basically just said this, but I’m posting it anyway since there’s the URL to the exception page

The thing that screamed at me that this was an out-of-date class file is the V at the end of the stack trace. Without going into too much detail, that basically signifies incompatible methods.

EDIT: From the AbstractMethodError page:

classic n00b mistake. Thanks ra4king (and lhkbob). The problem was runEffect() in Attack’s subclasses. They all had an undefined method in them, which made them unusable by the compiler so it went up to the parent class version which was abstract hence, the AbstractMethodError.

I use JCreator, because my first professor used it and I’ve grown accustomed to it. Eclipse and netbeans both take extremely long to startup and I don’t consider myself in need of their advanced features just yet. Everytime I build a project in JCreator, it saves all of the files that are open in JCreator. I thought it compiled them as well, but lesson learned. Thanks everybody.

Understandable :slight_smile:

By the way, I recommend using JGrasp www.jgrasp.org. It is an excellent IDE made in Java for Java :stuck_out_tongue:
I use it and it is way simpler and easier to use :slight_smile: