Answer the question
In order to leave comments, you need to log in
Why is there no abstract method in the ServiceProvider abstract class?
Hello!
I am learning Laravel Framework 5.7.15
And on the example of the Illuminate\Support\ServiceProvider class, the following question arose: what is the point of making the class abstract if it does not have a single abstract method?
Answer the question
In order to leave comments, you need to log in
I won’t speak for this class specifically, I didn’t work with laravel, but in general this makes sense.
An abstract class forces us to create its child classes, but also contains some generic code. By itself, an abstract class as an instance is useless, because does not contain specific code.
So, when we want to call some specific code from a generic code, we declare an abstract method.
But! When the logic of the algorithm involves calling a generic code from a concrete one, an abstract method is not needed. Moreover, we cannot say in advance which method(s) will be created. It turns out an abstract class, with protected methods, which does not contain abstract methods.
Making such a class non-abstract and making its methods public is not an option, because this provokes a violation of encapsulation.
In general, I sort of figured it out, thanks for the answers.
In my head, indeed, the concepts of "abstract class" and "abstract method" did not exist without each other until today.
I'll try to supplement Igor Vorobiov's answer
With this approach, an abstract class provides a basic implementation of a method.
The derived class can either use the base functionality or overload the method.
The client code will only depend on the abstract class.
The specific implementation of the method will be determined at runtime - late static binding (similar to virtual methods).
Something like this. Or not? :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question