Trying to get real concept of abstract class vs Interface in asp.net

I looked through numerous S / O messages and did not find an answer that helps me. I would like to get a mental vision of what an abstract class is and what an interface is. I read this post simple examples of using an abstract class in the real world , but I still do not quite understand these concepts.

I hope that someone will be able to describe the real world in the form of an object "Man." Such an inheritance would be “Man” → “Employee” → “Manager”
And Overriding will be “employee salary” will become “employee sales commission”

How can I describe an abstract class and interface in the concept of a Person object?

+6
source share
3 answers

There are different views on the use of an interface against an abstract class. The interface, however, must express the behavior of the object (what it can do), and the abstract class must determine what it is. In principle, “I can” compared to “I” from the object that pursues.

So, if we have a Person, this is a noun, so we will use an abstract class to define it. Any thing that "is" will inherit from this class. If we want to define any behavior that describes any behavior that a Person is capable of, does not apply to all Persons, we must put this in the interface.

Using the relationships you defined (Person → Employee → Manager), we can say that Employee implements IFirable, and Manager implements IFirer. An employee can be fired, and a manager can fire an employee.

+9
source

I apologize that I am not using your example with a person, please do this only as an extended comment.

It’s easiest for me to explain concepts to people who don’t have programming, using the analogy with what most people already understand: plastic molding!

Suppose we produce plastic fruits, for example:

The interface is similar to the form, it has the form of the final product, but you can fill it with all kinds of different colors and materials. They will all have the same shape at the end, even if they are completely different in color and texture.

An abstract class will be more like something that requires an additional step, for example, for drawing. You create basic plastic fruits, then send them to draw some kind of painting or fur or something like that.

The unfinished fruit is like an abstract class in which there is a greater definition of the final product than mold, but it is not complete in itself. This requires more work to be completed, and the final products may be completely different colors, but they are still basically the same.

I hope this helps!

+4
source

Ok, see if I understand your question ...

I think your concept is completely unaware of the differences and / or similarities between using interfaces and abstract classes. If this is a problem, then my resume is "starter for 10",

Abstract classes cannot be created , but they can contain implementation information , so when you derive a class from one, you get a contract and any default behavior that you put in an abstract class, for example, your Person class (I'll take its abstract class for this example) defines the contract that the derived class Employee or Manager should (at least) have, but can also provide some default behavior.

Interfaces on the other hand define only the contract ; that implementation classes must fulfill, they cannot provide any implementation method . For example, if the “Face” in your example was an infact interface, it should determine which classes implement it (Employee and Manager), but cannot provide a default implementation for this contract.

Also note that in many languages ​​(Java, C #, Delphi) you can inherit only one parent class, but you can implement many interfaces.

+4
source

Source: https://habr.com/ru/post/900701/


All Articles