简体   繁体   中英

How to implement an abstract function from a super class with an interface in java

So I have a class that looks something like this:

public abstract class GameObject {

    public abstract boolean hasValidLocation();

    //some more code that will use hasValidLocation
}

and an interface:

public interface Collidable {

    //some abstract references to functions of the game object

    default boolean hasValidLocation() {
        //checks whether or not the the game object has  a valid locaton
    }
}

and I have a similar interface for NotCollidable and would like to implement the abstract function hasValidLocation from the game object with this interface:

public class GameObject1 extends GameObject implements Collidable {
    //some code
}

but java says that GameObject1 does not implement hasValidLocation . I can't use 2 abstract classes since I already split gameObject in a DynamicGameObject and StaticGameObject and those can both be collidable and not collidable.

Is there something I did wrong or an alternative solution that doesn't require me to write hasValidLocation() multiple times?

I missed part of the question.

Perhaps your base abstract class should implement a super-interface common to Collidable and NotCollidable :

For example:

interface LocationValidatable {
    default boolean hasValidLocation() {
      return false;
    }
}

interface Collidable extends LocationValidatable {
    default boolean hasValidLocation() {
      return true; // replace with actual logic
    }
}

interface NotCollidable extends LocationValidatable {
    default boolean hasValidLocation() {
      return false; // replace with actual logic
    }
}

abstract class GameObject implements LocationValidatable {
    //some more code that will use hasValidLocation
}

Now, each concrete sub-class can choose whether to implement Collidable or NotCollidable :

class GameObject1 extends GameObject implements Collidable {
    //some code
}

class GameObject2 extends GameObject implements NotCollidable {
    //some code
}

Why not do something entirely different? Whenever you use the hasValidLocation method you can just check if the GameObject actually implements the Collidable interface.

Here is the code for the GameObject class:

public class GameObject
{
    // The GameObject actually does not contain a hasValidLocation() method.
}

And here is the code for the Collidable interface.

public interface Collidable
{
     // The Collidable interface actually contains the hasValidLocation() method.
     public abstract boolean hasValidLocation();
}

Here is just a quick example how you can determine if the gameObject implements the Collidable interface.

if (gameObject instanceof Collidable)
{
    final boolean isValid = ((Collidable) gameObject).hasValidLocation();
}

default methods are not abstract thus you need not to override it.

Thus you just need to override the abstract method in GameObject class.

So when you override like this

@Override
   public boolean hasValidLocation()
   {
      // TODO Auto-generated method stub
      return false;
   }

this is overriding method in GameObject interface

You can solve this problem simply by changing the name of one of the hasValidLocation methods.

For example

public interface Collidable {


    default boolean calculateLocationValidity() {
        return true;    
    }

}

and:

public class GameObject1 extends GameObject implements Collidable {

    public boolean hasValidLocation() {
        return calculateLocationValidity();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM