I’ve got class A that implements IA.
Now I need to create class B that should implement also IA. Class B has instance of class A as a member.
Is there any way to define that A’s instance implements the IA in class B?
interfase IA {
void method1();
void method2();
.
.
.
.
void methodN();
}
class A:IA {
public void method1(){}
public void method2(){}
.
.
.
.
public void methodN(){}
}
class B:IA {
private IA m_a;
public B(IA a) {
m_a=a;
}
//Instead all of this I am looking of a way to define that m_a is the implement to IA of B
public void method1(){ use of a.method1 }
public void method2(){ use of a.method2 }
.
.
.
.
public void methodN(){ use of a.methodN }
}
If
Bis really supposed to implementIA, thenBmust redefine each of the interface methods one by one, even if each implementation is simply a call to the implementation of the encapsulatedAmember.Nevertheless, there is a lazy way which can prevent you from all this tedious stuff and which can be considered almost as the same, from a practical point of view :
CatOwnerdoesn’t really implementCrysince the cat owner is not the one who meows : his cat does. But as an approximation we could consider that by demanding to the cat owner to cry, we of course mean that this demand actually targets his cat, not the owner itself. Then we “cast the cat owner to his cat” and then we can make himCry.That’s pretty funny, no ? 🙂
Edit :
That said, Magnus’ answer is highly worth considering IMHO. It appears more logical and more clean if passing a member is fine considering the semantic context. My solution may be still interesting if
Bis just a kind of enhanced variety ofAwhich cannot be inherited (sealed), or in such a particular context… Really depends on the context and the semantic constraints…