Proper use of Java Interface

Jonathan DeLorenzo :

I originally created an interface containing all the methods that would be shared between two classes, however, I realized I wanted both classes to have the same methods, but they are going to behave differently.

They will have the same return type, but different parameters. I can't figure out how to implement this, or even if I did figure out how to implement this, I don't know if it would be the proper way to handle the situation

Basically, I have come here looking for the correct architecture approach to my what I am trying to accomplish and I don't know what that would be. I think I have 4 questions for determining code architecture:

  1. Is an interface the right approach here, if so why?
  2. Is an abstract class the right approach here, if so why?
  3. This seems like it would be a common theme with OOP, by that I mean having a function you what to behave differently given a particular class. How should code be designed?
  4. Lastly, my first thought was, "Oh, I will just override the method in one of the classes", but that is giving me a huge headache and is not working. I feel like I never have this trouble when trying to override methods. Is overriding a method from an interface more complex?

public interface Character {
    public void setAttack();
}

/*the setAttack method here will be set by the programmer. The 3 values 
 passed by the programmer are then stored into an array*/
public class Player implements Character {
  public void setAttack(int x, int y, int z) {

    attackArray[0] = x;
    attackArray[1] = y;
    attackArray[2] = z;
  }
}

/*the setAttack will still serve the same purpose as the setAttack in the 
 player class, however, the values will be auto generated randomly once the 
 setAttack function is called for the NPC instance.*/

/*Another thought I had is passing the function that auto generates the 3 
integer values (numGen()) as a parameter 3 times, however, I'm not sure if 
this is possible. Just a thought*/


public class NPC implements Character {
  public void setAttack(){

      for(int i = 0; i < attackArray.length; i++)
    {
        attackArray[i] = numGen();
    }
  }
}
GhostCat salutes Monica C. :

There is a conceptual misconception: having the same methods means more than implementing methods that have the same name.

When you use polymorphism and interfaces in Java, then you are expressing intent. As in: classes implementing some interfaces must provide the corresponding "functionality". In other words: this describes a certain behavior.

Thing is: when the interface has foo(), and different classes might want a foo(X), and foo(Y) then the real question is: do these methods have more in common than just the name?!

If so, one possible solution would be another layer of abstraction. In your case, like:

public interface AttackParameters {
...


public interface Character {  
  public void setAttack(AttackParameters parms);

or something alike. The idea is to again replace "specific" details with a generic solution.

Alternatively, you could use a Map<String, Object> argument for your setAttack() method. In other words: attack parameters are identified by strings.

That is nice and dynamic, but well, it also sidesteps compile time safety. A slightly better version of the map approach would not use Strings as keys, but some enum class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=85807&siteId=1