multiple-inheritance

Inheritance from multiple interfaces with the same method name

半世苍凉 提交于 2019-11-26 12:19:55
If we have a class that inherits from multiple interfaces, and the interfaces have methods with the same name, how can we implement these methods in my class? How can we specify which method of which interface is implemented? By implementing the interface explicitly, like this: public interface ITest { void Test(); } public interface ITest2 { void Test(); } public class Dual : ITest, ITest2 { void ITest.Test() { Console.WriteLine("ITest.Test"); } void ITest2.Test() { Console.WriteLine("ITest2.Test"); } } When using explicit interface implementations, the functions are not public on the class.

How do Java Interfaces simulate multiple inheritance?

时间秒杀一切 提交于 2019-11-26 12:02:16
I am reading "The Java Tutorial" (for the 2nd time). I just got through the section on Interfaces (again), but still do not understand how Java Interfaces simulate multiple inheritance. Is there a clearer explanation than what is in the book? Peter Tillemans Suppose you have 2 kinds of things in your domain : Trucks and Kitchens Trucks have a driveTo() method and Kitchens a cook() method. Now suppose Pauli decides to sell pizzas from the back of a delivery truck. He wants a thing where he can driveTo() and cook() with. In C++ he would use multiple inheritance to do this. In Java that was

Calling parent class __init__ with multiple inheritance, what's the right way?

♀尐吖头ヾ 提交于 2019-11-26 11:40:49
Say I have a multiple inheritance scenario: class A(object): # code for A here class B(object): # code for B here class C(A, B): def __init__(self): # What's the right code to write here to ensure # A.__init__ and B.__init__ get called? There's two typical approaches to writing C 's __init__ : (old-style) ParentClass.__init__(self) (newer-style) super(DerivedClass, self).__init__() However, in either case, if the parent classes ( A and B ) don't follow the same convention, then the code will not work correctly (some may be missed, or get called multiple times). So what's the correct way again?

Multiple Inheritance from two derived classes

时光总嘲笑我的痴心妄想 提交于 2019-11-26 11:20:32
问题 I have an abstract base class which acts as an interface. I have two \"sets\" of derived classes, which implement half of the abstract class. ( one \"set\" defines the abstract virtual methods related to initialization, the other \"set\" defines those related to the actual \"work\". ) I then have derived classes which use multiple inheritance to construct fully defined classes ( and does not add anything itself ). So: ( bad pseudocode ) class AbsBase { virtual void init() = 0; virtual void

Objective-C multiple inheritance

本秂侑毒 提交于 2019-11-26 11:11:47
I have 2 classes one includes methodA and the other include methodB. So in a new class I need to override the methods methodA and methodB. So how do I achieve multiple inheritance in objective C? I am little bit confused with the syntax. Objective-C doesn't support multiple inheritance, and you don't need it. Use composition: @interface ClassA : NSObject { } -(void)methodA; @end @interface ClassB : NSObject { } -(void)methodB; @end @interface MyClass : NSObject { ClassA *a; ClassB *b; } -(id)initWithA:(ClassA *)anA b:(ClassB *)aB; -(void)methodA; -(void)methodB; @end Now you just need to

Are defaults in JDK 8 a form of multiple inheritance in Java?

此生再无相见时 提交于 2019-11-26 10:30:08
问题 A new feature coming in JDK 8 allows you to add to an existing interface while preserving binary compatibility. The syntax is like public interface SomeInterface() { void existingInterface(); void newInterface() default SomeClass.defaultImplementation; } This way for all existing implementations of SomeInterface when they upgrade to this new version they don\'t all suddenly have compiles errors around newInterface() . While this is neat, what happens when you are implementing two interfaces

C++ inherit from multiple base classes with the same virtual function name

允我心安 提交于 2019-11-26 10:01:38
问题 I tried this code: class A { virtual void foo() = 0; }; class B { virtual void foo() = 0; }; class C : public A, public B { //virtual void A::foo(){} //virtual void B::foo(){} virtual void A::foo(); virtual void B::foo(); }; void C::A::foo(){} void C::B::foo(){} int main() { C c; return 0; } It is OK when using the commented part, but when I try to write the definitions outside the class declaration, the compiler reports errors. I am using the MSVC11 compiler, does anyone know how to write

A use for multiple inheritance?

ぐ巨炮叔叔 提交于 2019-11-26 09:44:57
问题 Can anyone think of any situation to use multiple inheritance? Every case I can think of can be solved by the method operator AnotherClass() { return this->something.anotherClass; } 回答1: Most uses of full scale Multiple inheritance are for mixins. As an example: class DraggableWindow : Window, Draggable { } class SkinnableWindow : Window, Skinnable { } class DraggableSkinnableWindow : Window, Draggable, Skinnable { } etc... In most cases, it's best to use multiple inheritance to do strictly

How can interfaces replace the need for multiple inheritance when have existing classes

核能气质少年 提交于 2019-11-26 09:23:06
问题 First of all... Sorry for this post. I know that there are many many posts on stackoverflow which are discussing multiple inheritance. But I already know that Java does not support multiple inheritance and I know that using interfaces should be an alternative. But I don\'t get it and see my dilemma: I have to make changes on a very very large and complex tool written in Java. In this tool there is a data structure built with many different class objects with a linked member hierarchy. Anyway.

Inherit interfaces which share a method name

混江龙づ霸主 提交于 2019-11-26 08:55:39
问题 There are two base classes have same function name. I want to inherit both of them, and over ride each method differently. How can I do that with separate declaration and definition (instead of defining in the class definition)? #include <cstdio> class Interface1{ public: virtual void Name() = 0; }; class Interface2 { public: virtual void Name() = 0; }; class RealClass: public Interface1, public Interface2 { public: virtual void Interface1::Name() { printf(\"Interface1 OK?\\n\"); } virtual