For my Java class, we are learning about interfaces, polymorphism, inheritance, etc.
The homework assignment I am working on is a memory game where you have pairs of cards all face down and you turn over two at a time looking for a match. If they match, they stay visible, if they don’t, the cards are turned back over and you pick two more cards.
My design so far is as follows:
public interface Hideable
public abstract void hide();
public abstract void show();
public interface Speakable
public abstract String speak();
public interface AnimalCard extends Hideable, Speakable
public abstract boolean equals(Object obj);
public class Animal implements AnimalCard
public void hide() { ... }
public void show() { ... }
public boolean equals(Object obj) { ... }
// What do I do for the speak method since a generic Animal
// can't speak, but I have to provide a definition since the
// Animal class is implementing the interfaces.
public class Puppy extends Animal
// Here is where I need to define the speak method.
public String speak() { ... }
My question is in the comments above. I feel like I’m implementing this incorrectly with regard to the speak() method.
Just make the
Animalclassabstract.Concrete classes will have to implement
speak(), or beabstractthemselves.There’s no need or value in declaring an
abstractmethod forspeak()inAnimal– that method is implied by the class hierarchy.