Is .NET inheritance useless?

I am new to .NET programming. I know that .NET programming is 100% oriented. The intriguing paragraph that I read in a book about ASP.NET 4 states that

Inheritance is less useful than you might expect. In a typical application, most classes use restraint and other relationships instead of inheritance, since inheritance can make life harder, without bringing many benefits. Dan Appleman, a well-known .NET programmer, once described inheritance as "the coolest function you almost never use."

I'm a little confused here, and I need .NET programmers to tell me what I should take and what I should leave.

Edit

Please, people understand my question, at first the author did not literally say “Inheritance in .NET is useless”, it was my way to fix the question about a hole in some words so that it matches the name. Secondly, a book from Apress and its title: “The Beginning of ASP.NET 4 in C # 2010” on page 72.

+4
source share
10 answers

Uch. Inheritance is not only useful, but it is also a basic concept, since each class inherits from System.Object (not sure if there are CLR hacks / things that don't).

Also in some technologies, such as ASP.net MVC, you will almost certainly use inheritance (by creating a base controller).

Everyone uses Inheritance all the time, most of the time, without realizing it. Most smaller / medium sized applications do not have much inheritance; instead, interfaces are often used, for example, for Injection Dependency. However, almost all non-trivial applications tend to have a base class floating somewhere that other classes inherit from.

I assume that this means the author, the decision between the definition of the base class or interfaces. Microsoft recommends base classes, but in real life, interfaces seem to be more common. In addition, since C # does not support multiple inheritance (class A, which has more than one base class), interfaces provide more flexibility because they cannot centralize common code or use certain constructors.

+3
source

I think the inheritance presented in most intro-object-oriented academic classes is completely useless. You will always see hierarchies for Animal or Shape , but most real problems are not like .

And both of these examples break up quickly. Take a look at Linnaeus' classic biology classification or this Rectangle Shape article to see what I mean. Any inheritance hierarchy will collapse after a certain number of levels, because leaf classes depart from IS-A support with a base class.

Any model is a choice of what to leave and what to exclude. Inheritance can certainly work for some models and not very well for others.

Examples of where inheritance works well are data structures and interfaces. The interface says that method signatures are inherited, leaving implementation details. Therefore, if you can abstract away what is common to all List or Set or Map implementations, you have something that provides structure but leaves implementation details to the designers.

I would say that inheritance works very well in these cases.

Like most of the more complete statements, the one you have indicated is not universal.

+8
source

Inheritance is an OO concept that differs very little from .NET than in any other OO language, except that .NET allows single inheritance (only for classes); which is also quite common.

The author is right that inheritance is generally easy to misuse, and that encapsulation is usually the best option, but that doesn’t apply to .NET.

There are many times when it makes sense to use inheritance, and you should not shy away from it; polymorphism, etc. is a prime example. But most standard "data in data" does not need polymorphism.

He is, valuable - no, significant (at least to preserve sanity, you can, of course, avoid it if you like to do something hard). Feel free to ignore or use it.

But make your decision. Personally, I think the author is confused and may be a bit shortsighted.

+6
source

Given that you admitted that you are not familiar with the subject, as well as the fact that the book you are reading is well published, it seems likely that you simply misunderstood the alleged message. Perhaps the writing of this paragraph is not very clear. Inheritance is extremely useful and widely used.

+2
source

As one very small example, if there was no inheritance, when you created the form in windows or ASP.Net WebPage in a web application, you would need to add all the code for all the “windows” (for example, below **) and add this code to each such form or web page that you have created ...
Useless??? Hardly!

examples of some functions that you inherit from System.Windows.Form "
(Moving a window around, resizing, minimizing, maximizing, restoring, drawing a screen under it and next to it, processing messages generated by mouse clicks on it, controlling whether it has focus, transferring data back and forth between it and other elements screen, etc. etc.)

+2
source

I think most of what needs to be said here has already been said by people with much more experience than me. However, as the person who switched from VB6 / VBA to Endless And WOnderful World of True Inheritance, available on .net, I can say that discouraging a newcomer to see him as a final solution is not necessarily a bad thing.

When I first began to study this idea, I found all kinds of cool things that could be done through inheritance. But in the end, I ended up with pretty bad code, complicated to maintain class hierarchy and general mess. This seems like an almost magical solution for a beginner (and even MORE for those who have adopted the pain of VB6 / VBA for many years). The reality is that, like design patterns, inheritance is often used "because I can" instead of "because I need to."

I am sure that the author intended to convey the point of view that inheritance, often seen as a “first-order” solution for those who have less experience, should REALLY be used only when it is the right solution. And what is easy to think is that this may be the right decision in the wrong circumstances.

It may be tempting to use inheritance as a way to make coding easier. In the early years of training vb.net, of course, I had some difficulties, and often inheriting from a class that was a struggle for creation seemed to be an attractive solution to the problem of creating a different flavor of this complex code. Of course, for those who study, today's complex code is that it prefers code.

Inheritance is a core concept in OOP. But it is easily abused and / or improperly applied. The careful design of the class hierarchy is crucial for the proper use of the concept of inheritance, and those are most likely the most definitely cases where containment and delegation are preventive ways to achieve the goal.

But to say that inheritance is useless, is obviously absurd.

For a good way to learn interface / base class / inheritance modeling, look at the .net structure. From the object explorer, you can check the Inheritance model, because it applies to the main components that we use every day.

+2
source

On average, there is no business case; you will not create deep type hierarchies. However, you will use inheritance directly all the time .

Always.

The ASP.NET.aspx code-named page inherits from your code class. The code-code class itself is inherited from the Page class, and you override its methods. If you do not understand the meaning of redefinition and virtual (both concepts related to inheritance), you are selling yourself short.

There are thousands of examples in the whole structure where an understanding of inheritance and related concepts will help you figure out which objects you use daily. Some of the best ones to really understand LINQ and Generics (IEnumerable, IQueryable, ICollection, etc.), MVC / Web Forms and Entity Framework and where you create POCO objects that relate to the business problem that you are trying to solve.

The short answer is that you need to understand this, even if you are not writing a framework. The transition from the book ( excerpt from page 72 ) is a bit confusing as to how stupid and misleading it is.

+1
source

As I worked on more and more code, I came to similar conclusions (although not so provocative). One reason is the lack of multiple inheritance (which I now agree is good), and the other is dependencies that get out of hand.

These days, I try to avoid inheritance and abstract classes and rely more on composition and interfaces (especially now that extension methods can narrow the gap a bit to provide basic methods for interfaces).

This leads to significantly more reusable and verifiable code and greatly simplifies dependency management (using dependency inversion).

+1
source

Apparently, SO'ers overly responsive objections aside, this is a fairly common knowledge among object-oriented programmers these days.

I slightly disagree with the exact choice of the word from the quote, but the mood is nothing more controversial than “preferring songs by inheritance” .

In particular, for a book called The Beginning of ASP.NET , I think this is pretty decent advice. The number of times you need to deal with inheritance as a development engine in ASP.NET is quite limited - especially as a beginner.

+1
source

There are already many excellent answers, however, it’s hard for me to understand OOP while I studied it myself, so this can help.

First, just take a look at this article , it shows how you will use the class, and then inheritance in a regular asp.net application.

See this link for a real example of inheritance. His class is Web Control in asp.net . This is a class with the usual for many controls, scroll down and look at the inheritance hierarchy and see how many controls inherit from this control.

0
source

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


All Articles