20

what is the purpose of interface when writing a class?

heres an example i've seen online.

<?php
interface Chargeable {
    public function getPrice();
}

class Employee implements Chargeable {
    protected $price;

    public function getPrice() {
        return $this->price;
    }
}

$product = new Employee();

?>

7 Answers7

33

Here is one of the ways how I learned about interfaces and understood them.

Imagine this scenario:

abstract class Plane {
    public function openDoors();
}


interface Fliers {
    public function fly();
}

Now lets use them:

class Boeing747 extends Plane implements Fliers {
    public function fly() {
        // some stuff
    }

    public function openDoors() {
        // do something
    }
}

And:

class Tweety implements Fliers{
    public function fly() {
        // some stuff
    }
}

Boeing747 is Plane that can fly and Tweety is a bird than can fly but it makes no sense for Tweety to "openDoors".

The point is that interfaces can be implemented by different kinds of objects but classes can not. As you can see Boeing747 and Tweety have nothing in common other than both can fly.

Statik Stasis
  • 308
  • 1
  • 5
  • 16
Gabriel Sosa
  • 7,897
  • 4
  • 38
  • 48
  • 1
    I agree this is a very useful answer. Would love to see it edited for clarity. – Darren Newton Jul 04 '09 at 19:09
  • couldnt the same be said if their(Fliers=open door & Plane=Fly) function is reversed and use Class Plane instead of Fliers for function fly. So class tweety extends Plane to fly. While class Boeing747 implements Fliers and extend Plane to open doors and fly respectively. – Muhammad Umer Mar 01 '13 at 08:24
  • only way i can think of is that it'd help save memory as interface can't have nothing defined and abstract class can. – Muhammad Umer Mar 01 '13 at 08:26
11

An interface is a concept in Object Oriented programming that enables polymorphism. Basically an interface is like a contract, that by which classes that implement it agree to provide certain functionality so that they can be used the same way other classes that use the interface

Your example shows classes that guarantee that they have the getPrice method available. You can then write code that take advantage of objects that have this method without worrying about what kind of class it is.

Bjorn
  • 69,215
  • 39
  • 136
  • 164
0

Interfaces allow you to separate the interface from implementation. This is handy when you want to have orthogonality in your code among other things.

Basically you will be able to create functions that accept a Chargeable and be able to pass any object in there as long as it implements Chargeable. This allows you to be flexible down the road if you need to change the Employee class. Also it allows your method to accept any object that is "chargeable".

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
0

In languages with multiple inheritance instead of interfaces you have abstract classes. In PHP there is no multiple inheritance, so you have interfaces. One class can implement various interfaces. The only point is to guarantee that your class has certain set of methods.

vartec
  • 131,205
  • 36
  • 218
  • 244
0

I'm struggling to grok this myself at the moment (and I think I'm reading the same book as the OP...).

It seems to me that interfaces simply enforce a "contractual" obligation on classes that implement the interface to implement functions/properties that appear in the interface. Yet, isn't it the case that classes/objects which implement the interface can do so in a unique fashion since the implementation is not defined?

There is no actual inheritance involved is there?

sunwukung
  • 2,815
  • 3
  • 41
  • 56
-2

Firstable, interfaces structures provide interface to classes implemented for communicating outside world. Easy example would be a television. Television is a class and buttons on it are interfaces.

adv of using interfaces:

1-Java does not support multiple inheritance. If we want to add two different methods from different classes we cannot (extend ClassA,ClassB) is not possible implements A,B no problem.

2-The other adv is a security and flexibility An example can be given to be more specific if we want that some of methods of the class are not reachable how can we do that? polymorphism + interface we can do that if we do not want walk and bark methods should be unreachable

abstract class Animal {
    String name;
    int age;

    public Animal(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

}

class Dog extends Animal implements Run {

    public Dog(String name, int age) {
        super(name, age);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }

    public void bark() {

    }

}

class Cat extends Animal implements Climb {

    public Cat(String name, int age) {
        super(name, age);

    }

    @Override
    public void climb() {
        // TODO Auto-generated method stub

    }

    public void walk() {

    }

}

public class Main {

    public static void main(String[] args) {

        // if we want that some of methods of the class are not reachable how
        // can we do that?
        // polymorphism + interface we can do that
        // for example if we do not want walk and bark methods should be
        // unreachable

        Climb cat = new Cat("boncuk", 5);
        Run dog = new Dog("karabas", 7);

        // see we cannot reach the method walk() or bark()
        dog.walk(); // will give an error. since there is no such a method in the interface.

    }

}}

    enter code here
huseyin
  • 1,367
  • 16
  • 19
-3

let me give you example to understand the the dimnation of this type of classes

public interface transport{
public double getSpeed(){return(0/*the speed*/);}
public void setSpeed(){return(0/*the speed*/);}


}   
class car extend transport{
//implementation of transport interface
public double getSpeed(){return(speed/*the speed*/);}
public void setSpeed(){speed=250;}
}
class train extend transport{
//implementation of transport interface 
....

}

class plane extend transport{
//implementation of transport interface 
....
}

so the interface class is the general concept .

Ian Jacobs
  • 5,456
  • 1
  • 23
  • 38
hassan
  • 3
  • 1