multiple inheritance Flashcards

1
Q

how to incorporate multiple inheritance in c++?

A

class A: public B1, public B2 {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how do data members in interfaces work?

A

any data members in an interface are automatically public, static, and final (like constants)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how to incorporate multiple inheritance in java?

A

not possible for classes, but classes can have multiple interfaces (also interfaces can extend other interfaces)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what if 2 interfaces provide the same constant?

A

user will need to specify which version it is (X.NUM or Y.NUM)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what are 3 ways to simulate multiple inheritance in java?

A

1) interfaces

2) duplicate code (bad solution)

3) use limitation - insert objects lower into hierarchy, override methods we dont want (bad)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

in c++, what happens if a subclass with 2 super classes A and B both have the same method? how to call one or the other?

A

need to call it explicitly:

C c;
c.B::foo();

  • not a problem if method signatures are different
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what the diamond problem?

A

say we have a hierarchy where A is a superclass of both B1 and B2, and C is a subclass of both B1 and B2:

then, c has 2 copies of A’s data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

for the diamond problem with A, B1, B2, and C, how to call B1’s version of a protected field “a”?

A

BA::a = 6;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

in the diamond problem for C++, what if i dont want 2 copies of A in C?

A

inherit A virtually in B1 and B2:
class B1: public virtual A{…}

  • now C is responsible for creating A’s members in C
How well did you know this?
1
Not at all
2
3
4
5
Perfectly