Abstract vs Interface

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Ebybymic (Talk); changed back to last version by LukeRobinson)
 
Line 1: Line 1:
----
 
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 
----
 
=[http://ejahibuko.co.cc Under Construction! Please Visit Reserve Page. Page Will Be Available Shortly]=
 
----
 
=[http://ejahibuko.co.cc CLICK HERE]=
 
----
 
</div>
 
 
We all know that we should [[Program_to_the_interface_not_the_implementation | program to the interface not the implementation]]. But what ''is'' an interface? and how do we know what kind of interface to use?
 
We all know that we should [[Program_to_the_interface_not_the_implementation | program to the interface not the implementation]]. But what ''is'' an interface? and how do we know what kind of interface to use?
  
Line 15: Line 7:
 
The question is should I use an abstract base class or implement an interface? After all, they are both programing to the interface not the implementation.
 
The question is should I use an abstract base class or implement an interface? After all, they are both programing to the interface not the implementation.
  
In the example below, each &quot;Agent&quot; has one communication device, but it may be a cellphone, computer or pager. To contact an agent, we can just pass the message we want to send to their communication device.  
+
In the example below, each "Agent" has one communication device, but it may be a cellphone, computer or pager. To contact an agent, we can just pass the message we want to send to their communication device.  
  
 
[[Image:interface.png]] [[Image:Abstract.png]]
 
[[Image:interface.png]] [[Image:Abstract.png]]

Latest revision as of 03:08, 25 November 2010

We all know that we should program to the interface not the implementation. But what is an interface? and how do we know what kind of interface to use?

You might think the answer is simple, and many times it is, but there is a case where the line becomes hard to distinguish.

That case is known as Abstract vs Interface.

The question is should I use an abstract base class or implement an interface? After all, they are both programing to the interface not the implementation.

In the example below, each "Agent" has one communication device, but it may be a cellphone, computer or pager. To contact an agent, we can just pass the message we want to send to their communication device.

Interface.png Abstract.png

But which is the right design to use? The one with an abstract base class or the one with an interface?

There is no one rule about which to use, but here are some things to think about when you decide which to use in your design.


Interface:

Allows you to have a different base class, good for Java.

Lets objects that have a different main purpose fill the role needed.

Abstract:

Base class allows default actions, only override the methods needed.

Build well defined class structures, this can help simply overall design.


Some good ways to tell which you should be using are:

Do you need default actions? if so you probably need to have an abstract base class, because these are not supported by interfaces. Another way of looking at it is are all your methods abstract? If so then you don't need an abstract base class and can just go with an interface.

Personal tools