2 Replies - 336 Views - Last Post: 11 March 2012 - 11:13 PM Rate Topic: -----

#1 rvbsanjose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-October 11

Newbie help with abstract and interface -- Why & When

Posted 10 March 2012 - 11:12 PM

Hey all,

Back again with a newbie question. I've only been programming for 3 months now and I really want a career change into software development so I need all the expert advice I can get. At any point I am wrong, PLEASE correct me!!

At the end of chapter 14 of the book I'm reading, I ran into abstract classes and interfaces. By basic understanding is that abstract classes can have protected and public visibility while interfaces can not, only public. Constants are allowed in both, while interfaces do not allow properties or static methods. Abstract classes can have non abstract methods while interfaces basically can only have abstract methods (no block of code). There are other things that make them different, but this is not why I am posting.

So my question is why and when would you use interfaces over abstract classes? If interfaces are basically abstract methods that are to be implemented into a class that implements the interface and sub classes that extends abstract classes are also required in the sub class, why would you use interface at all given they have less flexibility than abstract classes?

Is This A Good Question/Topic? 0
  • +

Replies To: Newbie help with abstract and interface -- Why & When

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Newbie help with abstract and interface -- Why & When

Posted 11 March 2012 - 05:01 AM

Interfaces are perhaps most useful when you're working with a plugin infrastructure where others can contribute plugins to your system.

For example, you have a system that processes payments using any number of backends (Paypal, SagePay, WorldPay, whatever), so you could write an interface for each payment gateway, and have them submit their own implementations of it, for example:
interface IPaymentGateway
{
    public function Process($amount, $from);
    public function Refund($amount, $to);
    public function Authorize($amount, $from);
}


.. then your code will instantiate objects that are plugins implementing this interface, and call those functions because they will always be available no matter who the backend provider is.

Abstract classes are also useful for plugins, but could more commonly be used (depending on your application) to share common code among objects. Errr possibly the most simple example of this would be an animal, dog/cat:
abstract class Animal
{
   protected abstract function getSound();
   public function speak()
   {
       echo $this->getSound() . '!';
   }
}

class Dog extends Animal
{
   protected function getSound() { return 'Woof'; }
}

class Cat extends Animal
{
   protected function getSound() { return 'Meow'; }
}


A more real-world usage example would be that you are creating a database abstraction layer / object mapper, and have common functionality across all drivers (ie. database engines), but at the same time they all do things differently so you can't use the same code for everything, just some things.
Was This Post Helpful? 2
  • +
  • -

#3 rvbsanjose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-October 11

Re: Newbie help with abstract and interface -- Why & When

Posted 11 March 2012 - 11:13 PM

Thx RudiVisser. The explanation helped! :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1