Interfejsi i klase?

Wrong Turn

Domaćin
Banovan
Poruka
4.812
Nemam neke potrebe u PHP-u da radim sa OOP, ali me je uvek i interesovalo zasto bih koristio interfejs uopste?

On ne radi ništa, on je samo ugovor kog se nasleđene klase moraju pridržavati, što je isti ****** ko i abstraktna klasa?

Postavlja se pitanje zašto bih ja uopšte koristio interfejs ispred klase?
I koja je razlika interfejsa i abstraktne klase, čitao sam tonu tekstova o tome i nikako da skontam..
 
Odgovor sam našao na uvek neugodnom stekoverflovu naravno, de bi drugo, u vas se čovek pouzdati ccc...

The key technical differences between an abstract class and an interface are:

Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.

Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default).

When inheriting an abstract class, a concrete child class must define the abstract methods, whereas an an abstract class can extend another abstract class and abstract methods from the parent class don't have to be defined.

Similarly, an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation.

A child class can only extend a single class (abstract or concrete), whereas an interface can extend or a class can implement multiple other interfaces.

A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility (public).

tj na SrBskom:

Ključna razlike abstraktnih klasa i iterfejsa su:
Abstraktna klasa može da ima konstante, članove, metode (bez tela tj implementacije) i definisane metode, dok interfejsi mogu samo da imaju konstane i metode.
Metode i članovi abstraktne klase mogu biti definisani sa bilo kojom vidljivošću (private,public,protected itd), dok interfejsi moraju biti definisani kao javni. (podrazumevani su kao javni)
Kada se nasleđuje abstraktna klasa, dete klasa mora imati definisane abstraktne metode, abstraktne klase mogu naslediti drugu abstraktnu klasu i abstraktnu metodu od roditelja koja ne mora biti definisana.
Slično, interfejs koji nasleđuje drugi interfejs nije odgovaran za implementacijom metoda roditelja. To je zato jer interfejsi ne mogu ostvariti ikakvu implementaciju.
Dete klasa može naslediti jednu klasu (abstraktnu ili concrete), intefejsi mogu naslediti ili implementirati više interfejsa.
Dete klasa može definisati abstraktne metode sa istom ili manjom vidljivošću, dok klasa koja implementira interfejs mora definisati metode sa istom vidljivošću.


Zašto uopšte koristiti interfejs?

One specific example: interfaces are a good way of specifying a contract that other people's code must meet.

If I'm writing a library of code, I may write code that is valid for objects that have a certain set of behaviours. The best solution is to specify those behaviours in an interface (no implementation, just a description) and then use references to objects implementing that interface in my library code.

Then any random person can come along, create a class that implements that interface, instantiate an object of that class and pass it to my library code and expect it to work. Note: it is of course possible to strictly implement an interface while ignoring the intention of the interface, so merely implementing an interface is no guarantee that things will work. Stupid always finds a way! :-)

Another specific example: two teams working on different components that must co-operate. If the two teams sit down on day 1 and agree on a set of interfaces, then they can go their separate ways and implement their components around those interfaces. Team A can build test harnesses that simulate the component from Team B for testing, and vice versa. Parallel development, and fewer bugs.

The key point is that interfaces provide a layer of abstraction so that you can write code that is ignorant of unnecessary details.

The canonical example used in most textbooks is that of sorting routines. You can sort any class of objects so long as you have a way of comparing any two of the objects. You can make any class sortable therefore by implementing the IComparable interface, which forces you to implement a method for comparing two instances. All of the sort routines are written to handle references to IComparable objects, so as soon as you implement IComparable you can use any of those sort routines on collections of objects of your class.

Da skratim,
interfejsi su dobar način da drugi programeri ispune zahteve, nasleđujući tvoj interfejs oni moraju imati metode definisane u baznom interfejsu.
 
Poslednja izmena:

Back
Top