Does a subclass or child class also inherit the interface of its parent class?

0

Following the proposed encapsulation pillar in OOP, I have the following doubt, when it is declared a daughter class, besides inheriting attributes and methods, does it also inherit the interface of its mother?

interface ContaUsuario {
    public carregue($id,$nome);
}
class conta implements ContaUsuario {
   protected $id;
   private $nome;

   public carregue($id,$nome){
      $this->id = $id;
      $this->nome = $nome;

   }
}

When I declare the student class beyond it inherits this 2 attributes and 1 function does it also inherit the interface or would I have to re-create it?

    
asked by anonymous 07.05.2017 / 01:43

1 answer

3

When you define the interface in a parent class, the child class will also have the same effect. That is, if the Conta class implements the ContaUsuario interface, the Aluno class, which inherits Conta , will also implement the interface. Here's a simple test:

interface ContaUsuario {
    public function carregue($id, $nome);
}

class Conta implements ContaUsuario {
   protected $id;
   private $nome;

   public function carregue($id,$nome){
      $this->id = $id;
      $this->nome = $nome;

   }
}

class Aluno extends Conta {}

$aluno = new Aluno();

echo "Instância de Aluno: " . (($aluno instanceof Aluno) ? "Sim" : "Não"), PHP_EOL;
echo "Instância de Conta: " . (($aluno instanceof Conta) ? "Sim" : "Não"), PHP_EOL;
echo "Instância de ContaUsuario: " . (($aluno instanceof ContaUsuario) ? "Sim" : "Não"), PHP_EOL;

The output of the code is:

Instância de Aluno: Sim
Instância de Conta: Sim
Instância de ContaUsuario: Sim
  

See working at Ideone .

Related questions:

How do I know if a class implements an interface?

How and when to use Interface?

When to Use Interfaces

Abstract Class X Interface

In object orientation, why are interfaces useful?

    
07.05.2017 / 02:15