Author Topic: Abstract Classes and Interfaces  (Read 1700 times)

planetmarshalluk

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Abstract Classes and Interfaces
« on: January 16, 2005, 06:38:43 am »
What exactly is the difference between an abstract class and an interface?

An example would be great ( the canonical Shape->Circle example is at about my level... )

Cheers,
Andrew.

Simon M

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 6418
  • Karma: +55/-6
    • View Profile
Re: Abstract Classes and Interfaces
« Reply #1 on: January 16, 2005, 01:28:08 pm »
Simon

support@sparxsystems.com

sargasso

  • EA Practitioner
  • ***
  • Posts: 1406
  • Karma: +1/-2
  • 10 COMFROM 30; 20 HALT; 30 ONSUB(50,90,10)
    • View Profile
Re: Abstract Classes and Interfaces
« Reply #2 on: January 16, 2005, 02:28:04 pm »
An abstract class cannot be intantiated - but can implement methods and properties.  An interface is a template - it cannot implement anything.  

So interface iAnimal might prescribe 3 methods Move, Eat, Sound - any specific animal class must implement their own versions of Move, Eat , Sound for example for a duck:

void Move(origin, dest) {
case medium="land" {waddle(origin, dest)}
case medium="water" {paddle(origin, dest)}
case medium="air" {flap(origin, destination)}
}

for a cow
void Move(origin,dest) {
walk(origin,dest)
}

IOW interface methods are implemented by the interface implementor.


Abstract classes on the other hand can implement stuff.
Simplest example I can think of is the observer pattern.  The abstract  "Subject" class implements register(), deregister() and notfy().  Your concrete classes simply inherit these behaviors.  However, you cannot instatiate a Subject viz "mysub=new Subject();".  By extension, abstracts do not have creators etc,


hth
bruce  
"It is not so expressed, but what of that?
'Twere good you do so much for charity."

Oh I forgot, we aren't doing him are we.

Rob_M

  • EA User
  • **
  • Posts: 58
  • Karma: +0/-0
    • View Profile
Re: Abstract Classes and Interfaces
« Reply #3 on: January 24, 2005, 02:55:00 pm »
If you're a C++ guy, you can think of an interface as a class definition whose methods are ALL pure and virtual.

An abstract class is a class that contains at least one pure virtual function.

Tom_Andries

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
  • Unlearn you must.
    • View Profile
Re: Abstract Classes and Interfaces
« Reply #4 on: January 30, 2005, 09:18:38 am »
Wasn't there also something about a class having attributes, while an interface has none?

Although, seen from the outside that would make the difference nil, given encapsulation -:)

Tom Andries
     Associate
Method Innovation

Jamie Cash

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Abstract Classes and Interfaces
« Reply #5 on: March 07, 2005, 06:27:59 am »
An interface can have static members though. The only real difference is that an interface cannot contain implementation just a set of methods that should be implemented, although an interface can contain 0 methods and can be used as a container for constant data.