Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java abstract/extends issue

Tags:

java

oop

eclipse

I am currently in the process of developing a character generation mechanism for a Java based text game but I've ran into a issue and cannot see where I may have gone wrong. I have a "Character" class, which is abstract and then another class, "NPCharacter" which is meant build on top of this.

public abstract class Character {
    public abstract void generateStats();
}

public class NPCharacter extends Character {
    public void generateStats() {

    }
} 

Eclipse says that "The type NPCharacter cannot subclass the final class Character". Can anyone see the error here?

Thanks in advance.

like image 552
Ocracoke Avatar asked Feb 26 '12 15:02

Ocracoke


People also ask

What happens when you extend an abstract class Java?

In Java, abstract means that the class can still be extended by other classes but that it can never be instantiated (turned into an object).

Is it necessary to extend abstract class in Java?

A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

Do abstract classes extend object?

Yes abstract class does extends the object class. And it inherits properties of object class. We should not be confused here that abstract class can't be instantiate. That will happen in sub class of abstract class but in abstract class we can instantiate the object class and can do override the basic implementation.

Can an abstract extend an abstract Java?

Yes! But it makes sense only if the abstract subclass adds more functionality (abstract or not).


1 Answers

The compiler is confusing your Character with java.lang.Character. Just use a package and make sure you import your Character.

package com.foo;

public abstract class Character {
    public abstract void generateStats();
}

and

import com.foo.Character;

public class NPCharacter extends Character {
    public void generateStats() {

    }
}
like image 108
adarshr Avatar answered Nov 14 '22 23:11

adarshr