java - Why use inheritance at all? -


I know that the question has been made, but under this assumption it always seems that heritage is at least occasionally

My question is this: Since you can complete anything with the object structure that you can do with classical heritage and its Since then, classical heritage is often misbehaved [1] and because the object structure gives you a representative obs Gives you the flexibility to change the crop runtime, Why use never classical heritage?

I understand why you would recommend inheritance in some languages ​​like Java and C ++, which do not provide convenient syntax for the delegation. In these languages, whenever you are clearly not wrong to do so, you can save a lot of typing using heritage. But other languages ​​like Objective-C and Ruby have presented both classical heritage and very sensational syntax for the delegation. Go programming language is the only anchor that is determined by my knowledge that classical heritage is more difficult than it and supports the delegation only for code reuse.

Another way to tell my question is: Even if you know that classical heritage is not wrong to implement a certain model, then why is it reason enough to use it instead of structure? ?

[1] Many people use classical heritage to achieve their polynomial rather than implementing an interface to their classes. Purpose of heritage is to reuse code, not polymorphism. Apart from this, some people use heritage to understand the "one-one" intuitive of relationship.

Update

I just want to make clear what I mean when I talk about heritage:

< P> I am talking about it. I am talking about inheriting a purely abstract base class which is similar to implementing an interface, for which I am not debating against the record.

Update 2

I think inheritance is the only way to achieve polymorphism i C ++. In that case it is clear why you should use it, my question is limited to languages ​​like Java or Ruby which provides specific methods for obtaining polymorphism (interfaces and duck typing, respectively).

If you see any other object that implements the same interface ("base" object) , You are basically genetically oriented in the heritage at the top of the structure, but (in most languages) with a lot of verbatim and boilerplate. The purpose of using the structure instead of heritage is so that you can only represent the behaviors that you want to make a representative.

If you want to use the object to use all the behavior of the base class, unless explicitly overridden, the simplest, at least verbose, is the simplest way to express it. is.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -