-4

All similar questions I've looked though here, mentions multiple interface inheritance. However, I am not sure how MII could be a workaround to the problem.

Lets say, I have two library class (My_Class_1 and My_Class_2) of different methods. Then I want to create a new class, that can use both of these classes' methods natively, like:

public class My_Application : My_Class_1, My_Class_2 {

     public My_Application(){
         method_from_Class1();
         smth_property_declared_in_My_Class_2 = "hello";
     }

}

However, that is not possible with C#. What are flexible workarounds, to extend/enrich class with other classes? In PHP, that is unbelievably simple, just in the top of the class we can:

use example_trait_1;
use example_trait_2;

I would rather not use interfaces; in my view, they have no relation to solving this problem.

p.s. I don't want to create initialize objects for those classes. I want them to be native part of the application class.

halfer
  • 19,824
  • 17
  • 99
  • 186
T.Todua
  • 53,146
  • 19
  • 236
  • 237
  • 6
    use interfaces if possible in that case or composition, but depends on the use-case – Ehsan Sajjad Mar 19 '18 at 17:20
  • You already answered your own question: `that is not possible with C#`. You can use extension methods to add new code, or implement interfaces, nothing else (well, you can always use partial classes, but that's just syntactic sugar). – Gusman Mar 19 '18 at 17:21
  • 4
    The languages that support multiple inheritance are **fewer** than those that don't. Inheritance is **not** a code reuse mechanism. It means that a class has **is-a** relation with another type. You can specify this easily with interfaces. If you want to reuse code, use other mechanisms like generics or delegating to other objects. – Panagiotis Kanavos Mar 19 '18 at 17:21
  • Instead of inheriting `My_Application` from those 2 classes you can have their objects in `My_Application` class and call their respective methods in `My_Application` class – Chetan Mar 19 '18 at 17:22
  • 3
    The reason most languages *don't* use multiple inheritance is that your Class1 and Class2 may both have the same method named `Method1()`. Which one are you going to call? What if they contain the same *field* or *property* named `Property1`? Which one would you set? – Panagiotis Kanavos Mar 19 '18 at 17:23
  • 2
    Sorry. It's a duplicate, despite your pleas to the contrary. – spender Mar 19 '18 at 17:24
  • Not sure why `Interface` wouldn't solve your issue. You can have a `BaseClass` that `implements` multiple interfaces. Then your `My_Application` inherits `BaseClass`. Same thing. – penleychan Mar 19 '18 at 17:26
  • @spender please, for the sake of respect, tell me how that absolutely different thing is solution to this??? – T.Todua Mar 19 '18 at 17:26
  • @12seconds PPL, i dont know maybe I'm crazy, but how it is solution? I have a bunch of ready methods/library. In case of interfaces, I have to code the logic (actual method) in the primary class. and I have to do it in all my other applicaitons too, instead just storing that method in 1 class, and using that method from other classes. – T.Todua Mar 19 '18 at 17:29
  • @PanagiotisKanavos are you serious? if you talk about windows XP notepad, then i dont know, but where on earth any compiler, that cant detect that problem? Compiler should just throw error and that's all. I have different kind of methods library which i want to reuse. – T.Todua Mar 19 '18 at 17:30
  • @spender I need the reusable library of methods/properties (which I will use in similar applications), and you tell me to use interfaces (where I have actually to re-code the logic everytime in base class). – T.Todua Mar 19 '18 at 17:32
  • 3
    @T.Todua ...because it's exactly the same question dressed up in different words. Just because you don't like the answers doesn't entitle you to a free duplicate. – spender Mar 19 '18 at 17:33
  • Like Panagiotis mentioned, **inheritance is not for code reuse** – Camilo Terevinto Mar 19 '18 at 17:35
  • @CamiloTerevinto what i should use for CODE REUSE like i described? – T.Todua Mar 19 '18 at 17:37
  • 2
    You might interpret the solution to this problem as being "now I have to repeat myself over and over in the base classes". Now your base classes are almost certainly taking on too many responsibilities. The real answer is to make your classes small, single purpose entities that take on a **single** responsibility, then to compose more complex behaviour out of these service entities. Inheritance isn't the only way to solve this problem, and it appears that the architecture of the language is trying to steer you away from it. If you don't like it, there are other languages. – spender Mar 19 '18 at 17:37
  • @spender thanks, but i have no idea how C# manages this simplest problem to have reusable methods/properties. In `php`, `c+` and etc, the solution is just two words (like `using MyClass2.php` or etc).. However, I appreciate your help. I just wanted to have the methods re-used in all my other projects. – T.Todua Mar 19 '18 at 17:39
  • If you don't like c#, go to java... Oh, wait, it has exactly the same fundamental "problem". The methods you describe sound like they belong to static classes. Please post a more concrete example – Camilo Terevinto Mar 19 '18 at 17:42
  • @CamiloTerevinto here- https://pastebin.com/raw/TrSLKsvJ – T.Todua Mar 19 '18 at 17:50
  • Can you not put those two classes into a class library and then reference it from your application? I don't understand why you need inheritance. – Chris Dunaway Mar 19 '18 at 18:05
  • In your example makes more sense that `my_common_class_2` inherits from `my_common_class_1` and then you can inherit from `my_common_class_2` in the rest of classes. But said that, wouldn't be better to just have the properties from `my_common_class_1' in `my_common_class_2` and set the data in the constructors? – Gusman Mar 19 '18 at 18:13
  • 2
    See [Composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance), [Code Smell: Inheritance Abuse](https://softwareengineering.stackexchange.com/q/12439). If you want absolute clarity on what to do for code reuse instead of inheritance, read [Dependency Injection in .NET](https://www.manning.com/books/dependency-injection-in-dot-net-second-edition). – NightOwl888 Mar 19 '18 at 18:15
  • @ChrisDunaway I have specific requirement for that, instead of referencing, i need to expand current class. thanks anyway! – T.Todua Mar 20 '18 at 07:07
  • @Gusman well, i cant describe fully, but no, both of those classes are same-level types, and I need both of them to be like native methods/properties. it's a special requirement of application. – T.Todua Mar 20 '18 at 07:08
  • @NightOwl888 thnx for your help. – T.Todua Mar 20 '18 at 07:08
  • 1
    @T.Todua I'd have to ask "are you serious?" myself. The [diamond problem](https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem) is **very well known**. The only excuse would be lack of experience with multiple languages or complex models. Notice how long the [list of mitigations](https://en.wikipedia.org/wiki/Multiple_inheritance#Mitigation) is. Notice how many of those languages *don't* have multiple inheritance but use other mechanisms. Also notice the languages with "last man standing" rules, essentially leaving things to chance. – Panagiotis Kanavos Mar 20 '18 at 08:24
  • 1
    @T.Todua as for extending your current class there are a lot of patterns and techniques that *don't* need multiple inheritance and its rigidness. The availability of generics even means that you *don't* need multiple inheritance. Eg the Adapter or Wrapper patterns can extend classes. A generic adapter doesn't *need* multiple inheritance to work. It can extend *multiple* classes too, instead of only a single one. You can build only 1 adapter class to server 10 other types instead of building 10 combination types. You can *combine* adapters as needed without changing the types too. – Panagiotis Kanavos Mar 20 '18 at 08:27
  • 1
    @T.Todua in any case, if you want specific answers, post a new question and *be specific*. Most likely other people have faced the same issues in the past, have come up with solutions that eventually became patterns. Perhaps you need an adapter? Extension methods? Or no extension at all, since the problem would be easier to solve by generating new types? Eg trying to use a DTO at the UI levelc causes a *lot* of problems which is why some applications have different types for DTOs, domain entities and viewmodels. Or you really need a strategy class – Panagiotis Kanavos Mar 20 '18 at 08:33
  • @PanagiotisKanavos nice answers. you can post them as answer, instead of comments. Actually, I am new to C# and dont know about the model of adapter/extensions. I just wanted to extend current class with other class's methods and properties, like I don in php. that's all i want exactly. – T.Todua Mar 20 '18 at 11:45
  • [This seems related](https://stackoverflow.com/q/10729230/472495). – halfer Mar 20 '18 at 12:52
  • @halfer OUH, finally someone appeared who could just understand what i wanted. thanks mate, yes, i wanted to say that. but ppl telling me about interfaces, where I cant store any method. – T.Todua Mar 20 '18 at 13:55

1 Answers1

0

As others mentioned, it isn't possible (because that's not what inheritance is), but it seems one workaround would be to have a public property of type Class1, inside a class that inherits Class2.

That also gets around the problem of "which class am I looking at now?" since you'd need to explicitly mention the property when you want something of type Class1.

And if you need to modify Class1 first, then you just create a separate class that inherits Class1 first, then have the public property be of that new type.

Katerine459
  • 465
  • 1
  • 3
  • 13
  • By doing that, you are transforming a is-a relationship to a has-a dependency. Not really the best answer – Camilo Terevinto Mar 19 '18 at 17:43
  • But a class can only be one other thing, anyway... From what I understand of the question, he's trying to use inheritance to reuse code. In that context, it doesn't matter if it is-a, or has-a (in fact, logically, it's likely that he probably doesn't want inheritance at all, and only wants has-a relationships). – Katerine459 Mar 19 '18 at 17:45
  • The only logical situation I can think of, where it's reasonable to say that something is multiple other things, is, say, "Buyer is a Person AND a Shopper." But a Shopper is a person. So you just have a Shopper class that inherits Person, and have Buyer be a Shopper. – Katerine459 Mar 19 '18 at 17:54
  • @Katerine459 well, yes, i have class that has a requirement, to have other classes's methods and properties like a native (similar to inheritance). in `PHP`, we just use `using xyz` and that's all, you can include as many classes as you want. thanks for your time. – T.Todua Mar 20 '18 at 07:14