multiple-inheritance

How do I implement multiple inheritence in Java

烈酒焚心 提交于 2019-11-28 10:02:27
问题 I'm working with a certain API library in Java. It has a base class A, as well as B and C which both extend A. B & C provide similar but distinct functionality, all three classes are in the library. public abstract class A { virtual foo(); } public class B extends A {} public class C extends A {} How do I get elements of A , B , and C in my class? If I use interfaces to implement the classes, there is a lot of duplicate code, and inner classes will not allow me to override existing methods so

In C++, should I almost always use virtual inheritance?

百般思念 提交于 2019-11-28 09:39:38
I see from this entry that virtual inheritance adds sizeof(pointer) to an object's memory footprint. Other than that, are there any drawbacks to me just using virtual inheritance by default, and conventional inheritance only when needed? It seems like it'd lead to more future-proof class design, but maybe I'm missing some pitfall. The drawbacks are that All classes will have to initialize all its virtual bases all the time (e.g. if A is virtual base of B, and C derives from B, it also have to initialize A itself). You have to use more expensive dynamic_cast everywhere you use a static_cast

How does using interfaces overcome the problem of multiple inheritance in C#?

不羁岁月 提交于 2019-11-28 09:13:08
I understand that C# does not support multiple inheritance, and that the solution is to use interfaces instead. But what I don't understand is why interfaces doesn't create the diamond problem the same way as multiple inheritance would. How does using interfaces avoid the pitfalls of multiple inheritance? One class may implement any number of interfaces, even if those interfaces extend other interfaces as well. Multiple inheritance is not possible only with classes . // This is not allowed class A { void A() {} } class B { void B() {} } class C : A, B {} // This is allowed interface IA { void

Mixing virtual and non-virtual inheritance of a base class

强颜欢笑 提交于 2019-11-28 08:57:56
This is the code: struct Biology { Biology() { cout << "Biology CTOR" << endl; } }; struct Human : Biology { Human() { cout << "Human CTOR" << endl; } }; struct Animal : virtual Biology { Animal() { cout << "Animal CTOR" << endl; } }; struct Centaur : Human, Animal { Centaur() { cout << "Centaur CTOR" << endl; } }; int main() { Centaur c; return 0; } This code prints: Biology CTOR Biology CTOR Human CTOR Animal CTOR Centaur CTOR Why? Since we create a Centaur object, we start from building the Centaur by constructing Human , Animal and finally Centaur (we start from the less derived to the

“import” a definition of a function from a base class to implement abstract interface (multiple inheritance in C++)

那年仲夏 提交于 2019-11-28 08:51:54
问题 Say we have a class inheriting from two base classes (multiple inheritance). Base class A is abstract, declaring a pure virtual function foo , the other base class B declares and implements a function foo of the very same signature. struct A { virtual void foo(int i) = 0; }; struct B { virtual void foo(int i) {} }; struct C : public A, public B {}; I want to use the implementation of foo from base class B in my derived class C . However, if I do not implement the function foo a second time in

Multiple inheritance on Java interfaces

橙三吉。 提交于 2019-11-28 08:05:41
I thought multiple inheritance was always illegal in Java, but this code compiles: public interface A { void a(); } public interface B { void b(); } public interface AB extends A, B { } Would having an empty interface such as AB be considered a bad practice? Is there a way to achieve something similar while avoiding the empty interface (using generics or otherwise)? Note: I'm not asking how to simulate multiple inheritance via interfaces. I realize I could do the following: public class AbImpl implements A, B { public void a() {} public void b() {} } For various reasons I need an interface

Why is multiple inheritance not supported in most of programming language?

走远了吗. 提交于 2019-11-28 07:46:31
Why is multiple inheritance not supported in most of programming language? I could really use this feature to develop different layout of application? Multiple inheritance is useful in many situations as a developer, but it greatly increases the complexity of the language, which makes life harder for both the compiler developers and the programmers. One problem occurs when two parent classes have data members or methods of the same name. It is difficult to resolve which is being referenced by the sub-class. Another occurs when two parent classes inherit from the same base class, forming a

Multiple Inheritance: same variable name

淺唱寂寞╮ 提交于 2019-11-28 07:02:17
问题 class A { protected: string word; }; class B { protected: string word; }; class Derived: public A, public B { }; How would the accessibility of the variable word be affected in Derived ? How would I resolve it? 回答1: It will be ambiguous, and you'll get a compilation error saying that. You'll need to use the right scope to use it: class Derived: public A, public B { Derived() { A::word = "A!"; B::word = "B!!"; } }; 回答2: You can use the using keyword to tell the compiler which version to use:

In an abstract class constructor, why I do need to call a constructor of a virtual base that will never to called?

旧街凉风 提交于 2019-11-28 06:54:25
问题 I face the well known "dreaded" diamond situation : A / \ B1 B2 \ / C | D The class A has, say the constructor A::A(int i) . I also want to forbid a default instantiation of a A so I declare the default constructor of A as private . The classes B1 and B2 are virtually derived from A and have some constructors and a protected default constructor. [edit] The constructors of B1 and B2 don't call the default constructor of A . [reedit] The default constructors of B1 and B2 don't call the default

javascript inheritance from multiple objects

孤街醉人 提交于 2019-11-28 06:12:51
问题 I'm not very well aquainted with javascript inheritance, and I'm trying to make one object inherit from another, and define its own methods: function Foo() {} Foo.prototype = { getColor: function () {return this.color;}, }; function FooB() {} FooB.prototype = new Foo(); FooB.prototype = { /* other methods here */ }; var x = new FooB().getColor(); However, the second one overwrites the first one( FooB.prototype = new Foo() is cancelled out ). Is there any way to fix this problem, or am I going