multiple inheritance
-
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
-
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
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
andIClass2
then create your classesClass1
which implementsIClass1
andClass2
which implementsIClass2
. Then your class that "inherits" fromClass1
andClass2
implements both interfaces, relegating the method calls back toClass1
andClass2
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 -
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
andIClass2
then create your classesClass1
which implementsIClass1
andClass2
which implementsIClass2
. Then your class that "inherits" fromClass1
andClass2
implements both interfaces, relegating the method calls back toClass1
andClass2
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 1971So you can not do something like this? :confused:
class myClass:Class1 { } class Class1:Class2 { }
Nick Parker -
So you can not do something like this? :confused:
class myClass:Class1 { } class Class1:Class2 { }
Nick ParkerNick 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 -
So you can not do something like this? :confused:
class myClass:Class1 { } class Class1:Class2 { }
Nick Parker -
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 ThompsonThanks for clearing me up on this one Dale. Nick Parker
-
Thanks for clearing me up on this one Dale. Nick Parker
Sure thing. Dale Thompson