0

Possible Duplicate:
Interface vs Abstract Class (general OO)

Please explain to me, with an example, in what situation to use an Abstract class and Interface. In most scenarios people tend to use Interfaces rather than Abstract Classes in general, why? Explain the uses of them. Thanks in advance

Community
  • 1
  • 1
Rocky
  • 129
  • 2
  • 12
  • 3
    http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo – Marek Sebera Sep 05 '12 at 08:43
  • Multi-inheritance is illegal and interface is simpler at its nature. – d1e Sep 05 '12 at 08:43
  • 2
    Must be the most common question in Java and the most often answered, 2870000 results on google. – Peter Lawrey Sep 05 '12 at 08:44
  • In general use an interface if there is no common functionality to implement - just a contract to enforce. – RNJ Sep 05 '12 at 08:45
  • Interface is a "contract", while abstract class is a "skeleton". Please see http://stackoverflow.com/questions/2091893/pure-abstract-class-and-interface – Chao Zhang Sep 05 '12 at 08:46

1 Answers1

1

An abstract class is a special kind of class that cannot be instantiated. So, why we need a class that cannot be instantiated? An abstract class is only to be inherited from. In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain common behavior or properties across multiple classes who inherit the abstract class.

An interface is an entity that is defined by the word Interface. An Interface only contains signature of the methods whose implementation is to be provided by the classes who implements that Interface.

DadViegas
  • 2,263
  • 16
  • 12