A subclass or daughter class inherits the interface of its mother class as well?

Asked

Viewed 219 times

0

Following the pillar of encapsulation proposed in POO I arose the following doubt, when it is declared a daughter class, besides inheriting attributes and methods she also inherits the interface of her 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 it also inherits the interface or I would have to recreate it ?

  • she inherits the interface...

  • An account class that implements Contausuario ... !!! would be correct.

  • I don’t get it @Virgilionovic.

  • 2

    You’ll never inherit one interface, they are implemented ... Inheritance happens from classes Abstract (who may also have methods that are implemented) and Concretas!

1 answer

3


When you set the interface in a parent class, the daughter class will also suffer the same effect. That is, if the class Conta implements the interface ContaUsuario, the class Aluno, who inherits Conta, will also implement the interface. See a very 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 on Ideone.

Related questions:

How to 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 interfaces are useful?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.