简体   繁体   中英

How to implement a class that doesn't define all the abstract methods?

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 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.

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. )

you could make this kind of change

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

  public  abstract String speak();
 }

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() { ... }
}

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