Answer the question
In order to leave comments, you need to log in
In which case is it better to use an abstract class, and in which interfaces?
Give an example, in which case it is better to use an abstract class, and in which interfaces?
After all, the same task can be carried out both through interfaces and through an abstract class.
Answer the question
In order to leave comments, you need to log in
Just wanted to summarize previous answers:
An interface is just a list of methods, that is, in it we define what our methods will do. Like a black box: we give the input data and get the result, abstracting from how the result will be received.
Regular class (interface implementation) - here we define how our methods will work.
An abstract class is part of the abstract methods, because we cannot determine at this stage how they will be executed, the other part is the usual methods. Used in the following situations:
Suppose we have an interface and several of its implementations (ordinary classes): the interface will describe a method for calculating the definite Riemann integral, as well as a method for determining the accuracy of the result. There are several implementations: the method of rectangles, the trapezoid method, the Monte Carlo method. Let's create 3 regular classes for 3 methods, and inherit these classes from the interface. After implementation, you can notice that the method for determining the accuracy of the result is duplicated in all 3 classes, therefore it can be moved to the previous level of the hierarchy: we create an abstract class inherited from our interface, implement the second method in it, and leave the first one not implemented (abstract ). When adding a new calculation method, we no longer need to worry about the implementation of the method for determining the accuracy of the result.
Also used to prevent the creation of objects of this type. If a class is declared abstract we cannot create an object new AbstractClass();
Thus, an abstract class is a layer in the hierarchy between the higher-standing interface and lower-standing implementations.
An abstract class implements some of its methods, and some require implementation from descendants. The interface does nothing at all, all the logic is entirely shifted to the shoulders of the descendants.
UPD. Interfaces, IMHO, are convenient to use to describe gateways. That is, the application receives data from disparate sources in a variety of formats, and does not want to know anything about these formats. And abstract classes are good for describing related entities, for example, organizations with different forms of ownership (they all need a method to validate the TIN and request a registration address from the tax website). But in general, I repeat, the line is thin.
You need to create an interface if you are an architect, and in the future, other workers will inherit their classes only from this interface / s. Thus, you can control the scaling of the entire project.
Abstract classes are more of a special case that goes down the hierarchy.
If you need to specify the type of an object, then you must use an abstract class. And if you need to set behavior to an object, then you should use an interface.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question