1

I have a super-class, Animal, with two subclasses, Cat and Dog. Dog and Cat both need to have specific methods for speaking and moving. Here are two alternatives for achieving this:

  1. Animal will have two abstract methods, move() and speak(); Dog, Cat and Dog each override the two methods so they are specific to them.
  2. I could have an interface that has generic animal methods move() and speak(), with the two subclasses implementing the methods so they are again specific to them.

Is one of these approaches more appropriate than the nother? I realize if I had an ArrayList of animals, I would write:

ArrayList Animal<allAnimals> = new ArrayList <Animal>():
allAnimals.add(new Dog());
allAnimals.add(new Cat());

//If I override the methods, I can simply go: 
for (Animal a: allAnimals) a.move();

//Where as if I implemented the methods, I would not need be able to do this. I would need to cast the animal as its specific animal. e.g. If I knew the animal was a dog.

Dog aDog= (Dog) a; 
a.move();

So overriding and implementing could have certain advantages and disadvantages depending on the situation they are used in. Can anyone else elaborate on this ?

7 Answers 7

3

First, from the subclass point of view, there is no difference between overriding or implementing a method. So, the code that you wrote would work exactly the same in all cases (in Java, all methods act like C++ virtual functions; the method invoked is the one of the actual class being referenced).

So, the decission is to define a superclass that is an interface, an abstract class, or a instantiable class.

The instantiable class should be used if there makes sense to create objects of the superclass (new Animal()).

The abstract class, when there are no objects of the superclass but the implementation of some methods will be shared by all (or nearly all) the subclases.

The interface, when you only define the contract and the implementation is left to each subclass.

It depends of the scope of the problem to decide which use. For example, if move() just means "change x,y coordinates of animal", probably it should be an implemented method in an abstract class. But if you have to define how the animal moves, then Animal would be better an interface so each subclass defines it.

0
3

As a general rule of thumb, use extension (abstract classes), if and only if your extending classes will share a good amount of behavior. In that case, extension can be used eliminate the need for duplicate code, which is A Good Thing (TM).

On the other hand, if your classes simply need to have the same signatures, then go with an Interface, which is much more flexible (you can, for instance, implement several interfaces, but you can only extend from one class).

This is just somewhat generic advice, since every use case is different. To avoid the limitations of extension, there are other techniques that can be used, for instance, composition: (http://en.wikipedia.org/wiki/Composition_over_inheritance).

Last but not least, I think you're slightly mistaken in your code. You can call the methods on the base classes in exactly the same way, no matter if they're inheriting them from an Abstract Class or an Interface.

2
  • Thanks - great info - + rep to you if I could. Apr 14, 2013 at 21:46
  • You can, to me or to whoever you think answered your question best, by accepting. :)
    – pcalcao
    Apr 14, 2013 at 21:47
1

In this case it probably depends on whether or not you'd like to have a default implementation of move() so Dog and Cat don't HAVE to provide their own implementations. Presumably the move() method shares a lot of commonality between cats and dogs so you'd have better reusability with a common implementation in the superclass.

0
1
//Where as if I implemented the methods, I would not need be able to do this. I would need to cast the animal as its specific animal. e.g. If I knew the animal was a dog.

Dog aDog= (Dog) a; 
a.move();

No, you wouldn't have to do this at all. If Animal is an interface instead of an abstract class, the process you show for abstract classes would work just fine.

6
  • I was thinking of keeping Animal as an abstract class with common methods and properties, then move and speak would be implemented. Not sure what I would call it. If i did it this way, I would need to cast the object as its specific animal.. Apr 14, 2013 at 21:53
  • Not if you're only calling the methods defined in the abstract class or the interface. And it makes no difference whether it's an abstract class or an interface defining those methods.
    – Don Roby
    Apr 14, 2013 at 21:54
  • okay if I have an ArrayList of Animals (allAnimals) containing a dog and cat, then for dog and cat I implement Vocalizable which contains the speak method. I couldn't access the speak method by using for (Animal a: allAnimals) a.speak(); I would need to cast it as a dog or a cat, as they are the classes that know speak not the abstract class. That's my understand anyway - I could be getting confused by your meaning, please elaborate if Im wrong? Apr 14, 2013 at 22:00
  • You could cast it as a Vocalizable... Now I understand a bit more your conundrum, perhaps I'll update my answer.
    – Don Roby
    Apr 14, 2013 at 22:03
  • Not going to bother updating the answer,as your accepted answer is just fine.
    – Don Roby
    Apr 14, 2013 at 22:17
1

Conceptually, an abstract class describes what the thing IS, an interface describes what it DOES. In this case it's appropriate to have Animal as an abstract class because a Cat or a Dog IS an Animal. Functionally, choose whichever option results in the least amount of coding / the cleanest class hierarchy.

1

In general, the rule of thumb is to use an interface if you can cause a Java class can implement many interfaces but extend only one class.

Extending (inheritance) should be used only if there is a lot in common to a few classes and you want to be able to reuse the same code.

By the way, in the code that you provided as an example, you can use:

for (Animal a: allAnimals) a.move();

both in the case when you use interface as well as inheritance.

0
0

First of all, you have a third option, which is: Have an interface (say, MoverAndSpeaker; although that's kind of a lame name), and have the abstract base class Animal 'implmenent' it:

static public abstract class Animal implements MoverAndSpeaker {
    @Override
    public abstract void move();
    public abstract void speak();
}

Why would you even want something like that?

Unlike some of the answers here, I believe semantic consistency is the paramount consideration, and issues such as avoiding code duplication are either resolved on their own or take the minority in importance.

If I were in your position, the key criterion I would apply is:

"Are there objects which move and speak, which are not Animals?"

  • If the answer is "no", then moving and speaking is something Animals do. Thus, use the abstract methods only
  • If the answer is "yes", then I ask myself

"Do all animals move and speak?"

  • If the secondary answer is "yes", then use the above code (assuming you don't want some default implementation for all animals).
  • If the secondary answer is "no", consider just the interface. Although you might want an intermediary class between Dog, Cat and Animal - if you have some shared code or fields between all Animals which also move and speak.

Finally, if there's nothing in common to all animals, then as suggested in other answers, you might not need the Animal base class at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.