开发者

Abstract classes and interfaces in Java [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Use of Java [Interfaces / Abstract classes]

Being new to java, what are the differences be开发者_开发百科tween using an abstract class in your project and an interface ?


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.


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 ....


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜