0

Possible Duplicate:
When to use an interface instead of an abstract class and vice versa?
Difference between Interface, abstract class, sealed class, static class and partial class in C#?

public class Guru{
    public Enemy(int x, int y, int health, int attack, ...) {
        ...
    }
    ...
}
public class UserDefinedClass extends Enemy {
    ...
}
Community
  • 1
  • 1

3 Answers3

1

If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

  • An Interface cannot implement methods.
  • An abstract class can implement methods.

  • An Interface can only inherit from another Interface.

  • An abstract class can inherit from a class and one or more interfaces.

  • An Interface cannot contain fields.

  • An abstract class can contain fields.
giri77
  • 71
  • 3
0

An abstract class can not be instantiated but that can contain code whereas interface only contains method definitions but does not contain any code. you need to implement all the methods defined in the interface.

If your logic will be the same for all the derived classes, it is best to go for a abstract class in stead of an interface.

You can implement multiple interfaces but only inherit from one class.

Rahul
  • 76,197
  • 13
  • 71
  • 125
0

oAn interface implies the minimal coupling possible between the object and the code that wants to consume it. An abstract class implies some stronger relationship between the classes, and probably some commonality of implementation.

An interface should be used when we want to separate concerns as much as possible (eg Dependency Injection)

An abstract class should be used to model a common family of objects where a strong relationship exists in the domain

Aidan
  • 4,783
  • 5
  • 34
  • 58