V
V
vrazbros2017-03-14 09:32:55
OOP
vrazbros, 2017-03-14 09:32:55

Why can an interface only have public methods?

Hello
Why can only public methods be in an interface, and not, for example, protected too?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Shamsudin Serderov, 2017-03-14
@Steein

To get started
Go and read PHP OOP
public - Access is open from outside the class.
private - only from class methods.
protected - access from methods of an arbitrary class.
PS

class Example implements ExampleInterface
{
    /****
     * Я тебе везде пригожусь
    */
    public function test()
    {
        return $this->view();
    }
    
    /****
     * Я тебе нужен в этом классе, в другом месте хрен ты меня используешь.
     * 
     * @return string
    */
    protected function view()
    {
        return 'Привет ты меня использовал';
    }
}

interface ExampleInterface 
{
    public function test();
}

class InitExample 
{
    public function release(ExampleInterface $ex)
    {
        echo $ex->test();
    }
}

M
Mercury13, 2017-03-14
@Mercury13

Private - for what? After all, the one who goes to implement this interface will not see them.
With protected, things are more complicated. The fact is that in the classic interface there is zero code and data, and these protected should be called by - who - of course, descendants. It is premature to perpetuate the internal architecture - why?
There is also such a thing as a “pumped interface” - I don’t know how it is correctly called according to OOP. There are two new things there: 1) utilities - non-virtual functions that are a standard scenario for using the interface; 2) regular implementations - for some virtual functions, they come up with an implementation that somehow works and uses other virtual ones, but a descendant, if he wants, can write a more efficient version.
Here is an example utility.

// write Intel word
void st::Stream::writeIW(uint16_t w)
{
    write(&w, sizeof(uint16_t));
}
The utility is not part of the interface, and the best syntax for utilities is C# extension methods. At worst, a simple function like Streams.writeIW(stream, 10);.
Here, for example, regular implementation.
// Возвращает остаток потока
// cu_minus = clipped unsigned minus
virtual Pos remainder() { return cu_minus<Pos>(size(),pos()); }
If there are no regular implementations in the language, they build a class where these functions are implemented by something - not always, but often you can inherit from this class.
Since the utility uses the usual public functions, there is no point in throwing it into protected (theoretically, some parts of complex utilities can be private / protected). Regular implementations - all the more so, this is the same part of the interface as the abstract size() and pos().

S
SpyDeX, 2017-03-15
@SpyDeX

An interface is a contract.
by publishing it (even to himself) the programmer says what external methods/sv-va the object has.
This is necessary for planning the interaction of future parts that do not yet exist.
Protected privates and so on are of absolutely no interest to anyone at this stage, since this will only apply to classes that implement a specific interface.
The main thing is that the necessary pre-planned threads are pulled out, which are guaranteed to be present in the classes that implement the interface.
As soon as you think of protected interfaces, you start to move down the abstraction to the implementation. If you really need code discipline, you can make a base class with empty planned rotated methods, a la interface developer toolkit, but only after you have planned a working interface and without protected ones. And after that, ask to use only it in development as a base one, but this contradicts the whole paradigm, where interaction is rigidly prescribed, and the implementation and scope are not limited in any way

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question