-2

Can anybody explain the concept of interfaces with good example. I searched but didn't find a good answer. I'm still confused with these concepts.

I found an example for interface from Internet which is shown below. The code works without the interface with the same output. Then what is the purpose of it? Or is it not the real implementation of interfaces?. I need to move from procedural coding to object oriented programming. It would be better if anyone can explain not in more complex technical words.

 <?php
interface IPillage
{
public function emptyBankAccounts();
public function burnDocuments();
}

class  Employee 
{
public function emptyBankAccounts()
{
echo "Call employees and ask to transfer funds to Swiss bank account";
}
public function burnDocuments()
{
echo "Torch the office suite of the employee";
}

}

class Executive  extends Employee implements IPillage
{
public function emptyBankAccounts()
{
echo "Call executive and ask to transfer funds to Swiss bank account";
}
public function burnDocuments()
{
echo "Torch the office suite of the executive";
}

}
$obj1=new Employee();
$obj2=new Executive();
$obj1->emptyBankAccounts();
echo '<br>';
$obj2->emptyBankAccounts();
?>
Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
  • 1
    I have seen that post already but didn't understood it well. That is why I posted it as a new question. – Krishnadas PC Oct 10 '13 at 12:43
  • 1
    If you are just starting with object oriented programming, then you **don't** need to use interfaces. Interfaces should come later in your learning process. Their purpose is to define a protocol. If you have an interface with a single function called `query`, then every class that implements the interface will have that function. That way, you are **sure** that all classes stemming from your interface will follow a rule which is to have a function called `query`. As for why the need for interfaces occurred, it's beyond the scope of your question. – N.B. Oct 10 '13 at 14:42

3 Answers3

1

many languages just allow single inheritance, with the interfaces, you are allow to have a kind of multiple inheritance... you can inherit from a base class and in the same time from many interfaces as well. For example, you can have many classes like.. human, animal, truck. They are different of course, but they can implement an interface like "movable" with a method called "to move." In that case i can refere to three differents objects from the optical of a "movable element'. I can say myHuman.move/ myMonkey.move / my Truck.move.. I am sure that I can ask for move to any object that implements the movable interface with out take care about another things that the particular objects can do... To think about an interface I have to think about funcionality that they offer. Sorry about my english I am from Argentina. Nicolas Perichon

Pericles
  • 120
  • 7
0

Interface:

cannot be instantiated.is a special type of abstract class in which all the members do not have any implementations. enables polymorphism. A class can implement more than 1 Interfaces. Normally used for application classes: providing contract for ensuring interactibility.

the aim: making sure something is interchangeable. A class that implements an Interface need to contain all the implementation, otherwise the compiler will throw an error. CAN-DO relationship. e.g. Student CAN enrol, Student CAN submit assignment.

  public interface ICanEnrol
{
    void Enrol();
}

public interface ICanSubmit
{
    void Submit();
}

public class Student : ICanEnrol, ICanSubmit
{
    public void Enrol()
    {
        School.Send("enrolment");
    }

    public void Submit()
    {
        School.Send("report");
    }
}

public class Employee : ICanEnrol, ICanSubmit
{
    public void Enrol()
    {
        Company.Send("enrolment");
    }

    public void Submit()
    {
        Company.Send("report");
    }
}
Praveen D
  • 2,337
  • 2
  • 31
  • 43
  • What if we use different classes with same function without interfaces? That is my doubt. In my example the output is the same without interfaces also? – Krishnadas PC Oct 10 '13 at 13:28
  • 1
    You need to understand concept of interface. It is a contract for classes those implement it. – Praveen D Oct 11 '13 at 04:46
0

As PraDes wrote, creating an interface creates a sort of contract. That is really the key concept here.

Let's say that you are working on a project with a team of other developers. There are several new classes that need to be written and the team wants to come to a consensus on a common approach / design pattern. Everyone agrees that each of these classes will need a foo method, a bar method and a baz method. The concrete details of how these methods will work is going to be a little bit different in each class. However, they should each have the same signature.

We decide to create an interface which names the methods that each of these classes should have (foo, bar, baz) and what their signatures are (ie what params they take). Now when each of us writes one of these new classes we will "implement" the interface. By declaring that "MyClass" implements "MyInterface" we are now required to implement foo, bar, and baz methods in our class. If we do not, our IDE, our compiler, our runtime environment, etc will throw an error.

Neil Girardi
  • 4,533
  • 1
  • 28
  • 45