Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. multiple inheritance

multiple inheritance

Scheduled Pinned Locked Moved C#
oopquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mazdak
    wrote on last edited by
    #1

    I wanna declare class with multiple inheritance . public class myclass: System.class1 and System.Class2 How should I write the code that compiler accept it? Mazy "The path you tread is narrow and the drop is shear and very high, The ravens all are watching from a vantage point near by, Apprehension creeping like a choo-train uo your spine, Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd

    J 1 Reply Last reply
    0
    • M Mazdak

      I wanna declare class with multiple inheritance . public class myclass: System.class1 and System.Class2 How should I write the code that compiler accept it? Mazy "The path you tread is narrow and the drop is shear and very high, The ravens all are watching from a vantage point near by, Apprehension creeping like a choo-train uo your spine, Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      You can't, not directly anyway. You are limited to one base class, but you can implement as many interfaces as you desire. The recommended way is to have class1 and class2 be interfaces (or just one of them be an interface). The downside is that it makes it more difficult to extend existing functionality because you have to reimplement everything. I suppose one solution would be to have interfaces IClass1 and IClass2 then create your classes Class1 which implements IClass1 and Class2 which implements IClass2. Then your class that "inherits" from Class1 and Class2 implements both interfaces, relegating the method calls back to Class1 and Class2 objects if the base functionality is desired. And since you implement both interfaces you can cast the object to both interfaces as well. Its not pretty but it should work for the most part. James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

      N 1 Reply Last reply
      0
      • J James T Johnson

        You can't, not directly anyway. You are limited to one base class, but you can implement as many interfaces as you desire. The recommended way is to have class1 and class2 be interfaces (or just one of them be an interface). The downside is that it makes it more difficult to extend existing functionality because you have to reimplement everything. I suppose one solution would be to have interfaces IClass1 and IClass2 then create your classes Class1 which implements IClass1 and Class2 which implements IClass2. Then your class that "inherits" from Class1 and Class2 implements both interfaces, relegating the method calls back to Class1 and Class2 objects if the base functionality is desired. And since you implement both interfaces you can cast the object to both interfaces as well. Its not pretty but it should work for the most part. James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

        N Offline
        N Offline
        Nick Parker
        wrote on last edited by
        #3

        So you can not do something like this? :confused: class myClass:Class1 { } class Class1:Class2 { } Nick Parker

        D S 2 Replies Last reply
        0
        • N Nick Parker

          So you can not do something like this? :confused: class myClass:Class1 { } class Class1:Class2 { } Nick Parker

          D Offline
          D Offline
          Dale Thompson
          wrote on last edited by
          #4

          Nick Parker wrote: So you can not do something like this? class myClass:Class1 { } class Class1:Class2 { } Of sourse you can - that's single inheritance. But you can't do: class myClass: Class1, Class2 { } The "solution" is to do the following: class Class1 {     void foo(){}; } interface IClass2 {     void bar(); } class Class2: IClass2 {     void bar(){}; } class MyClass: Class1, IClass2 {     Class2 m_implClass2 = new Class2;     void bar()     {         m_implClass2.bar();     } } // lots of public keywords missing! Hope this helps! :) Dale Thompson

          N 1 Reply Last reply
          0
          • N Nick Parker

            So you can not do something like this? :confused: class myClass:Class1 { } class Class1:Class2 { } Nick Parker

            S Offline
            S Offline
            SimonS
            wrote on last edited by
            #5

            This you can do. I'm not really sure what this is called (baring a mini-object model). I understand multiple inheritance as: class mybase1 { } class mybase2 { } class mysuper:mybase1,mybase2 { } Which you can't do in C#. Cheers, Simon X-5 452 rules.

            1 Reply Last reply
            0
            • D Dale Thompson

              Nick Parker wrote: So you can not do something like this? class myClass:Class1 { } class Class1:Class2 { } Of sourse you can - that's single inheritance. But you can't do: class myClass: Class1, Class2 { } The "solution" is to do the following: class Class1 {     void foo(){}; } interface IClass2 {     void bar(); } class Class2: IClass2 {     void bar(){}; } class MyClass: Class1, IClass2 {     Class2 m_implClass2 = new Class2;     void bar()     {         m_implClass2.bar();     } } // lots of public keywords missing! Hope this helps! :) Dale Thompson

              N Offline
              N Offline
              Nick Parker
              wrote on last edited by
              #6

              Thanks for clearing me up on this one Dale. Nick Parker

              D 1 Reply Last reply
              0
              • N Nick Parker

                Thanks for clearing me up on this one Dale. Nick Parker

                D Offline
                D Offline
                Dale Thompson
                wrote on last edited by
                #7

                Sure thing. Dale Thompson

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups