How to declare an interface attribute?

1

How can I declare some attributes in an interface? For example, I have the interface called animal , I want it to have some attributes that the mamifero class will implement, but the attribute I declare on the interface does not work.

interface Animal {
   protected $peso, $altura; 
}

The above code generates syntax error in NetBeans.

    
asked by anonymous 07.12.2017 / 01:51

3 answers

4

What you really want is an abstract class and not an interface. If you really need it. Even by the name, an animal should not be an interface since this mechanism is to indicate abilities and not objects.

Inheritance is not something to use on anything , it has certain criteria. And a protected member is also rare to be useful when done right. Most of the time you use this you should not. usually this specific hierarchy of the example serves to show how inheritance works, but it is lousy to teach guidance to real objects. In biology it is very complicated to make hierarchies correctly, things are not as linear as they seem and modeling often does not work.

Understand more about abstract classes (there are links for other things, including explaining the difference to the interface.

I do not like the term attribute, I prefer field .

    
07.12.2017 / 02:10
0

Good evening, my friend.

This is a bit of an OOP concept, since an interface has to be a "purely abstract class", at most it contains method signatures to be implemented according to the need of the class that extends them.

Currently we have the concept of "Default Methods", where an interface can have concrete methods, that is, in its usual form, in some languages the attributes may even be declared, but this is not a recommendable practice. / p>     

07.12.2017 / 01:58
0

In your case, what you are looking for is an Abstract Class , not an Interface. I think it's important to make a brief distinction between Interfaces and Abstract Classes, since you seem to have some doubt in the difference between the operation of both. Remembering, both are concepts of Object Oriented Programming.

I will illustrate using the Java language, but the operation is similar in PHP:

Interfaces

They work as contracts signed between you and your class, where you present certain guarantees of class functioning; all classes that implement a certain interface must implement all methods (functions) declared in the interface, or the code will not compile. How methods are implemented, however, is not important, and there may even be an empty body of method.

The Caelum handout on Object Orientation brings

public interface Conta {
  public double getSaldo();
  public void deposita(double valor);
  public void saca(double valor);
  public void atualiza(double taxaSelic);
}

Next, we can create a class AccountCurrent that will implement the Account interface, thus ensuring that it will have all the basic methods of an Account, regardless of its specialty.

class ContaCorrente implements Conta {

  // outros atributos e métodos

  public double getSaldo() {...}
  public void deposita(double valor) {...}
  public void saca(double valor) {...}
  public void atualiza(double taxaSelic) {...}
}


class ContaPoupanca implements Conta {

  // outros atributos e métodos

  public double getSaldo() {...}
  public void deposita(double valor) {...}
  public void saca(double valor) {...}
  public void atualiza(double taxaSelic) {...}
}

The interesting thing about Interfaces is that you can create contracts with multiple interfaces, thus indicating that your class should perform functions linked to different concepts. For example, a Dog class can implement the Animal, Biped, and Mammalian interfaces. This is the way some programming languages use to implement Multiple Inheritance .

It is important to cite, however, that each class that implements the interface must declare its own implementation of the methods.

Abstract Classes

In short, they work like blueprints , that is, working templates of a class. Abstract Classes are one of the basic concepts of Inheritance . Abstract Classes can not be initialized by itself, and there should always be a concrete class that inherits from the abstract class. Abstract classes allow the declaration of variables and standard methods for all classes that inherit from it. Thus, taking the example of bank accounts mentioned above, you could declare standard data for all accounts (variables) and standard implementations of declared methods:

Note: For ease of example, I'll be using double to declare values, but you should not use double or float for values that require great precision because subsequent calculations with the variable precision can change its value along the way, unless you use methods to normalize values and reduce this variance.

public abstract class Conta {
  private double saldo;
  private String nome;
  private String sobrenome;
  private int idade;

  public double getSaldo() {return this.saldo;}
  public String getNome() {return this.nome;}
  public String getSobrenome() {return this.sobrenome;}
  public int getIdade() {return this.idade;}
  public void deposita(double valor) {this.saldo += valor;}
  public void saca(double valor) {
    if (saldo > valor) 
      saldo -= valor;
    else System.out.println("Não é possível sacar esse valor");
  }
  public void atualiza(double taxaSelic) {}
} 

If when implementing an interface the class is required to create its own implementation of the declared methods, when inheriting from an abstract class, the methods declared in the abstract class are already readily usable by the class that inherited it. The concrete class takes on the role of daughter, who can use everything that has been declared in her father.

Imagine your father has a car. The car is not yours, but you, as a child, can use the car to leave occasionally.

public class ContaPoupanca extends Conta {

  public ContaPoupanca (double saldo, String nome, String sobrenome, int idade) {
    this.saldo = saldo;
    this.nome = nome;
    this.sobrenome = sobrenome;
    this.idade = idade;
  }
  public void atualiza(double taxaSelic) {
    this.saldo *= 1 + taxaSelic;
  }
}

Unlike Interfaces, a class can inherit directly from only one other class. There is no way for a Dog class to inherit directly from Animal, Beep, and Mammal, since each of these classes may have method declarations with identical signatures, but different implementations, which would cause conflicts during program compilation.

    
07.12.2017 / 04:47