Skip to main content
removed links, replacing with code samples
Source Link
JUBEI
  • 27
  • 5

When I'm asked in job interview what's an abstract class and how it differs from regular class I reply: You can't create an object from an abstract class. First you need to extend it and it's a job of a regular class. They nod their head and go to next question.

That's a constraint and constraints help programmers think by narrowing down the choices they can make. The job of an abstract class is to always point "North". By "North" I mean responding to property/method calls from classes who (in your case) did not define method addArtikel.

Here is an example from PayPal repository where a class extends an abstract class:

https://github.com/paypal/PayPal-PHP-SDK/blob/ff2a7f9c2ddc47a22cb7bb8a5b0bb97bd16bdd6f/lib/PayPal/Log/PayPalLogger.phpclass PayPalLogger extends AbstractLogger

It so happens that this abstract class also implements a well known interface https://www.php-fig.org/psr/psrPSR-3/ interface.

Here is another example from PayPal where class both extends and implements: https://github.com/paypal/PayPal-PHP-SDK/blob/ff2a7f9c2ddc47a22cb7bb8a5b0bb97bd16bdd6f/lib/PayPal/Common/PayPalResourceModel.php

Here is an example of a loose interfaceclass PayPalResourceModel extends PayPalModel implements IResource

Digging deeper you find this: https://github.com/paypal/PayPal-PHP-SDK/blob/ff2a7f9c2ddc47a22cb7bb8a5b0bb97bd16bdd6f/lib/PayPal/Rest/IResource.php

<?php
namespace PayPal\Rest;
/**
 * Interface IResource
 *
 * @package PayPal\Rest
 */
interface IResource
{
}

Nothing? YesThis example is interesting, because nothing. Programmers have 1000 ways is actually asked of not stepping on their own feet (foot?)programmer who is going to implement this interface. Maximum freedom.

Now, the interface is like a map of a territory, or a todo list for a programmer. It reminds what requirements are. When implementing an interface programmer MUST define all methods listed inside interface file. It throws an error if you skip one. While it's not a case with abstract classes you see.

Going back to your "problem":

  1. AbstractArtikel should implement an interface IArtikel and define fallback functionality.
  2. ArtikelLP, ArtikelBook and ArtikelBoardgame should extend AbstractArtikel and redefine methods where appropriate. Here not only similarities, but also differences start to be important.
  3. Create ArtikelLP, ArtikelBook and ArtikelBoardgame objects.

Can an interface extend an interface? This I don't know.

When I'm asked in job interview what's an abstract class and how it differs from regular class I reply: You can't create an object from an abstract class. First you need to extend it and it's a job of a regular class. They nod their head and go to next question.

That's a constraint and constraints help programmers think by narrowing down the choices they can make. The job of an abstract class is to always point "North". By "North" I mean responding to property/method calls from classes who (in your case) did not define method addArtikel.

Here is an example from PayPal where a class extends an abstract class:

https://github.com/paypal/PayPal-PHP-SDK/blob/ff2a7f9c2ddc47a22cb7bb8a5b0bb97bd16bdd6f/lib/PayPal/Log/PayPalLogger.php

It so happens that this abstract class also implements a well known interface https://www.php-fig.org/psr/psr-3/

Here is another example from PayPal where class both extends and implements: https://github.com/paypal/PayPal-PHP-SDK/blob/ff2a7f9c2ddc47a22cb7bb8a5b0bb97bd16bdd6f/lib/PayPal/Common/PayPalResourceModel.php

Here is an example of a loose interface: https://github.com/paypal/PayPal-PHP-SDK/blob/ff2a7f9c2ddc47a22cb7bb8a5b0bb97bd16bdd6f/lib/PayPal/Rest/IResource.php

Nothing? Yes nothing. Programmers have 1000 ways of not stepping on their own feet (foot?).

Now, the interface is like a map of a territory, or a todo list for a programmer. It reminds what requirements are. When implementing an interface programmer MUST define all methods listed inside interface file. It throws an error if you skip one. While it's not a case with abstract classes you see.

Going back to your "problem":

  1. AbstractArtikel should implement an interface IArtikel and define fallback functionality.
  2. ArtikelLP, ArtikelBook and ArtikelBoardgame should extend AbstractArtikel and redefine methods where appropriate. Here not only similarities, but also differences start to be important.
  3. Create ArtikelLP, ArtikelBook and ArtikelBoardgame objects.

Can an interface extend an interface? This I don't know.

When I'm asked in job interview what's an abstract class and how it differs from regular class I reply: You can't create an object from an abstract class. First you need to extend it and it's a job of a regular class. They nod their head and go to next question.

That's a constraint and constraints help programmers think by narrowing down the choices they can make. The job of an abstract class is to always point "North". By "North" I mean responding to property/method calls from classes who (in your case) did not define method addArtikel.

Here is an example from PayPal repository where a class extends an abstract class:

class PayPalLogger extends AbstractLogger

It so happens that this abstract class also implements a well known PSR-3 interface.

Here is another example from PayPal where class both extends and implements:

class PayPalResourceModel extends PayPalModel implements IResource

Digging deeper you find this:

<?php
namespace PayPal\Rest;
/**
 * Interface IResource
 *
 * @package PayPal\Rest
 */
interface IResource
{
}

This example is interesting, because nothing is actually asked of programmer who is going to implement this interface. Maximum freedom.

Now, the interface is like a map of a territory, or a todo list for a programmer. It reminds what requirements are. When implementing an interface programmer MUST define all methods listed inside interface file. It throws an error if you skip one. While it's not a case with abstract classes you see.

Going back to your "problem":

  1. AbstractArtikel should implement an interface IArtikel and define fallback functionality.
  2. ArtikelLP, ArtikelBook and ArtikelBoardgame should extend AbstractArtikel and redefine methods where appropriate. Here not only similarities, but also differences start to be important.
  3. Create ArtikelLP, ArtikelBook and ArtikelBoardgame objects.

Can an interface extend an interface? This I don't know.

Source Link
JUBEI
  • 27
  • 5

When I'm asked in job interview what's an abstract class and how it differs from regular class I reply: You can't create an object from an abstract class. First you need to extend it and it's a job of a regular class. They nod their head and go to next question.

That's a constraint and constraints help programmers think by narrowing down the choices they can make. The job of an abstract class is to always point "North". By "North" I mean responding to property/method calls from classes who (in your case) did not define method addArtikel.

Here is an example from PayPal where a class extends an abstract class:

https://github.com/paypal/PayPal-PHP-SDK/blob/ff2a7f9c2ddc47a22cb7bb8a5b0bb97bd16bdd6f/lib/PayPal/Log/PayPalLogger.php

It so happens that this abstract class also implements a well known interface https://www.php-fig.org/psr/psr-3/

Here is another example from PayPal where class both extends and implements: https://github.com/paypal/PayPal-PHP-SDK/blob/ff2a7f9c2ddc47a22cb7bb8a5b0bb97bd16bdd6f/lib/PayPal/Common/PayPalResourceModel.php

Here is an example of a loose interface: https://github.com/paypal/PayPal-PHP-SDK/blob/ff2a7f9c2ddc47a22cb7bb8a5b0bb97bd16bdd6f/lib/PayPal/Rest/IResource.php

Nothing? Yes nothing. Programmers have 1000 ways of not stepping on their own feet (foot?).

Now, the interface is like a map of a territory, or a todo list for a programmer. It reminds what requirements are. When implementing an interface programmer MUST define all methods listed inside interface file. It throws an error if you skip one. While it's not a case with abstract classes you see.

Going back to your "problem":

  1. AbstractArtikel should implement an interface IArtikel and define fallback functionality.
  2. ArtikelLP, ArtikelBook and ArtikelBoardgame should extend AbstractArtikel and redefine methods where appropriate. Here not only similarities, but also differences start to be important.
  3. Create ArtikelLP, ArtikelBook and ArtikelBoardgame objects.

Can an interface extend an interface? This I don't know.