3

Please note. This question is not an abstract class vs interface kind of question.

Yes. I know. It's not necessary for a class which extends an abstract class to override all of its unimplemented methods. If a child class is not giving definition to some of its parent's unimplemented methods, then child class will be also considered to be abstract.

But a class which implements an interface should implement all of its methods ( Also Multiple inheritance is possible with interfaces).

Is this the only difference between an abstract class with only abstract methods and an interface?

yes, I understand. An Abstract class can have states and method implementations. But I'm making the question very clear. Its not actually a interface vs abstract class kind of question.

Here, in the question, the abstract class is not having any data members or any method implementations. Just some abstract methods only. eg:

abstract class shape{
    abstract void draw();
}

I just want to know whether there are any other differences. What should I use in such a scenario?

Please help.

Razib
  • 10,965
  • 11
  • 53
  • 80
Nidhin Joseph
  • 1,461
  • 3
  • 15
  • 32

3 Answers3

4

I believe your question is not necessarily one vs the other but how to know which one to choose in an instance.

A way I like to think about it is that interfaces tend to be actions that are available to the user to interface with the class. For example, if a class implemented the interface playable you know it would have a play method, whether it be starting an audio player, starting video, or starting a game would depend on the class itself, but you just know it is a playable class and can play.

Whereas shape is not an action, it is a type, shape comes along with attributes to define it, you need to know the color, size, etc. in order to know something is a shape. This is why I would use an abstract class to define all of the properties they have in common.

Because draw() is functionality that can apply to shapes, images, or scenes I would implement that as a Drawable interface.

Example:

 public class Square extends Shape implements Drawable{

      public void draw(){
          //draw code here
      }
 }

This way you can define all the common properties inside of Shape and provide the functionality to draw itself through the Drawable interface.

chrissukhram
  • 2,957
  • 1
  • 13
  • 13
2

Well I believe Interfaces have more uses in software design. You could decouple your component implementing against interfaces so that you have more flexibility on changing your code with less risk. Like Inversion of Control patterns where you can use interfaces and then when you decide you can change the concrete implementation that you want to use. Or other uses for interfaces is you could use Interceptors using interfaces (Unity Interceptor) to do different things where not all of these is feasible or at least as straightforward using abstract classes.

Aram
  • 5,537
  • 2
  • 30
  • 41
  • As I am not fully aware of the behavioral and structural design patterns, I was struggling to understand the whole idea. Anyways, I will look deep in to it. Thanks a lot for the right direction. – Nidhin Joseph Mar 28 '15 at 20:38
1

The situation you stated here (When both interface and abstract class has only the method signature and abstract class has not any partial implementation of method) I prefer interface even. Since the abstract class has not any specification of the draw() method and no field/property it is virtually an interface with the problem imposing your subclass not to inherit other necessary class. Consider the following code snippet -

public class AnySubClass extends Shape{}

Now the AnySubClass can't ever extends any other subclass. But if the Shape was an interface then AnySubClass can implements other interface, or may extends some other important class.

In general interface give you the freedom to implement the method - implements your own way, but must have to implement. But abstract class should be used in such a case when you want to provide some guideline to your subclass how to implement. And moreover you can implements multiple interface.

Razib
  • 10,965
  • 11
  • 53
  • 80