1

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.

5
  • 6
    Make it abstract too and let the first concrete implementation deal with the implementation of the still abstract methods. Commented Nov 1, 2012 at 22:06
  • @LuiggiMendoza I was under the impression that I can't declare an abstract method in a class, only in an interface. Am I wrong? Commented Nov 1, 2012 at 22:07
  • Yes, you're wrong. An abstract class, by definition, is a class that can contain abstract methods. It can't be instantiated, of course. See docs.oracle.com/javase/tutorial/java/IandI/abstract.html Commented Nov 1, 2012 at 22:08
  • Perfect. I wasn't aware I could define the Animal class as abstract in this context. Commented Nov 1, 2012 at 22:24
  • @Bart My bad, apologies. I guess I've spent too much time at math.stackexchange :-) Commented Nov 1, 2012 at 23:49

4 Answers 4

4

Just make the Animal class abstract.
Concrete classes will have to implement speak(), or be abstract themselves.

public class abstract Animal implements AnimalCard { public void hide() { ... } public void show() { ... } public boolean equals(Object obj) { ... } 

There's no need or value in declaring an abstract method for speak() in Animal - that method is implied by the class hierarchy.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thank you. I wasn't aware I could define a public abstract in that manner. But your explanation makes it clear.
1

the purpose of an interface is to guarantee that your class will behave in a certain way.

So if you declare a speakable object

Speakable speakingThing = new Puppy(); 

Whatever you put in that speakableThing variable must be able to do anything that the Speakable interfaces says it can do.


What I'd do, is not have AnimalCard implement Speakable and have only Animals that can speak implement the interface

Or, as other people said, if All your animals will speak, and you just will never instantiate the generic animal, than make your classes abstract.(abstract classes don't get instantiated. they're only there to be inherited from. )

Comments

0

you could make this kind of change

 public abstract class Animal implements AnimalCard{ public void hide() { } public void show() { } public abstract String speak(); } 

1 Comment

No need to add the speak method definition: it's already declared in the interface.
-1

First,

you cant have abstract methods in an interface. They are essentially abstract by default, since it is illegal to have any implementation code within the interface.

Second, java doesnt support multiple inheritance, so your line:

public interface AnimalCard extends Hideable, Speakable 

is illegal.

The best way to solve your problem once you fix those things is to make Animal card abstract.

public abstract Animal extends AnimalCard { public void hide() { ... } public void show() { ... } public boolean equals(Object obj) { ... } public abstract String speak(); } public class Puppy extends Animal { // Here is where I need to define the speak method. public String speak() { ... } } 

2 Comments

An interface can extend multiple interfaces. The AnimalCard interface is legal. And there's no need to redefine the speak() method, since it's already declared in the interface.
Your first sentence sort of collapsed on itself. ALL method declarations in an interface are abstract and public by default. If you do not explicitly type the abstract or public keywords, they will be implicitly placed there.