0

Possible Duplicate:
Use of Java [Interfaces / Abstract classes]

Being new to java, what are the differences between using an abstract class in your project and an interface ?

Community
  • 1
  • 1
Adrian
  • 19,440
  • 34
  • 112
  • 219
  • Have you checked Google or searched this site first? If so, what specifically is confusing to you? For instance, a quick check of this site shows hundreds of posts identical (or actually better -- with more detail in the question) than yours, such as: http://stackoverflow.com/questions/2869222/use-of-java-interfaces-abstract-classes – Hovercraft Full Of Eels Mar 11 '11 at 13:52
  • 6
    This is a duplicate of many, many other questions - see the "Related" column on the right. – razlebe Mar 11 '11 at 13:52
  • I don't think that this is an exact duplicate because this is a general question about the using. The other questions in the related column mostly are like "I have bla bla bla... what do you recommend and why" – Chris Mar 11 '11 at 14:01
  • @Chris: You are correct. The other questions are more specific and thus answerable in a forum format such as this. This question is much more general and vague and would require a full tutorial or book to answer correctly. – Hovercraft Full Of Eels Mar 11 '11 at 14:10

5 Answers5

3

You can only inherit from one class at a time, using the "extends" keyword, but you can implement as many interfaces as you want, using the "implements" keyword. Also, abstract classes can have both abstract and concrete (implemented) methods, as well as variables.

evilcandybag
  • 1,942
  • 17
  • 17
1

Interface doesn't contain any implementation. It just describes how the class, which implements the interface, can interact with other classes.

Abstract class can contain some methods, which are implemented and define abstract methods, similiar to interfaces.

The usage of classes and interfaces should be considered not to the whole project, but to particular places.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
1

A interface is a contract (no implementation), where an abstract class is both a contract WITH implementation.

http://www.java-tips.org/java-se-tips/java.lang/difference-between-abstract-classes-and-inter.html

Marcus
  • 1,866
  • 1
  • 20
  • 33
1

If you look at it less technically but how you can or should use it:

The main Advantage of an interface is that a class can implement as many interfaces as you like. In Contrast to that one class can only extend one single other class. (There is no multiple inheritance in java).

With using interfaces you can add single "Capabilities" to your classes. Therefor you will often read that interfaces names ends with "able". like "Serializable" or "Parceable" or something like that.

An Abstract class can be a general class which if forced to be extended. Like a "Vehicle" for example. You can't use a "Vehicle" itsself because there is no thing existing which is only a "Vehicle". So you have to implement a class extending that class which could be Cars or Boats ....

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Chris
  • 7,675
  • 8
  • 51
  • 101
0

In simple engilsh, an interface is a class where all methods are abstract but not implementable (in the interface). Only subclasses (which are not abstract classes) of those interface must implement the abstract method.

Abstract classes have some method implementations, but can contain abstract methods that must be implemented by concrete subclasses.

Wikipedia states (interface):

In object-oriented languages the term "interface" is often used to define an abstract type that contains no data but exposes behaviors defined as methods. A class having all the methods corresponding to that interface is said to implement that interface. Furthermore, a class can implement multiple interfaces, and hence can be of different types at the same time.

Wikipedia: (Abstract Class)

An abstract class, or abstract base class (ABC), is a class that cannot be instantiated. Such a class is only meaningful if the language supports inheritance. An abstract class is designed only as a parent class from which child classes may be derived. Abstract classes are often used to represent abstract concepts or entities. The incomplete features of the abstract class are then shared by a group of subclasses which add different variations of the missing pieces.

In java you extend a class/abstract class but you implement an interface.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228