Learn about Abstract and Interface

Tram Ho

1) Basic content:

A. Interface:

  • Syntax:

When used, the interface keyword is declared.

The subclasses that you want to use need the implements keyword.

  • Define:

Lets specify a class to implement.

Many classes use the same interface, called polymorphism.

B.Abstract:

  • Syntax:

The abstract keyword is used before the class.

The child class inherits by extends

When a subclass is inherited from an abstract class, we have the following rules:

  • The subclass method must be defined with the same name and it declares the parent abstract method
  • The subclass method must be specified with the same or less restrictive access modifier
  • The number of required arguments must be the same. However, a subclass can have additional optional arguments

2) Example:

A. Abstract:

B. Interface

In this example, it can be seen that a class implements from multiple interfaces. Quite interesting, right

3) Conclusion

It can be understood roughly, these 2 methods are used to create a framework for our project. Help us to work quickly.

A few references:

https://www.w3schools.com/php/php_oop_classes_abstract.asp https://www.php.net/manual/en/function.interface-exist

Thank you everyone for reading

Share the news now

Source : Viblo