Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to deal with double composition and inheritance?
    primarykey
    data
    text
    <p>I found this related question: <a href="https://stackoverflow.com/questions/279158/how-do-i-use-composition-with-inheritance">How do I use composition with inheritance?</a></p> <p>I would like to do the same with Objective-C, that is to say that a <code>GenericView</code> knows that its property <code>obj</code> is a GenericObject, and that a <code>SpecializedView</code> knows that the very same <code>obj</code> property is a <code>SpecializedObject</code>.</p> <p>Here is an example that will be clearer:</p> <pre><code>// // Example.m #import &lt;UIKit/UIKit.h&gt; /* HEADER */ // Electrical Machine @interface ElectricalMachine : NSObject { } - (void)plugIn; @end // Toaster @interface Toaster : ElectricalMachine { } - (float)getThermostat; @end // GenericView @interface GenericView : NSObject { ElectricalMachine *machine; } - (void)doSomethingGeneric; @property (nonatomic, retain) ElectricalMachine *machine; @end //SpecializedView @interface SpecializedView : GenericView { } - (void)doSomethingSpecialized; @end /* IMPLEMENTATION */ // GenericView @implementation GenericView @synthesize machine; - (void)doSomethingGeneric { Toaster *toaster = [[Toaster alloc] init]; [toaster plugIn]; self.machine = toaster; [toaster release]; } @end // SpecializedView @implementation SpecializedView - (void)doSomethingSpecialized { /* ERROR HERE * Incompatible types in initialization * 'ElectricalMachine' may not respond to '-getThermostat' */ float r = [machine getThermostat]; r = r; // ... } @end </code></pre> <p>As you see, I get an error at the end, because for <code>SpecializedView</code> the machine property is an <code>ElectricalMachine</code>, not a <code>Toaster</code>.</p> <p>Thank you very much for your help!</p> <p><strong>Old Question</strong></p> <p>Here is the first version of my question, which was maybe too cryptic:</p> <p>I have the following generic view:</p> <pre><code>@interface GenericView { GenericObject obj; } - (id)doSomething; </code></pre> <p>I also have the following specialized view:</p> <pre><code>@interface SpecializedView : GenericView { } - (id)doSomethingElse; </code></pre> <p>I have the following object:</p> <pre><code>@interface GenericObject { } - (id)plugIn; </code></pre> <p>and the following specialized object:</p> <pre><code>@interface SpecializedObject : GenericObject { } - (float)toastTime; </code></pre> <p>Let's say I want GenericView to handle GenericObject, and SpecializedView to handle the same object, knowing that it is SpecializedObject.</p> <p>Let me explain by showing implementations:</p> <p><strong>GenericView doSomething</strong></p> <pre><code>- (id)doSomething { [obj plugIn]; } </code></pre> <p><strong>SpecializedView doSomethingElse</strong></p> <pre><code>- (id)doSomethingElse { // ERROR here float time = [obj toastTime]; } </code></pre> <p>I will get the following warning: 'GenericObject' may not respond to '-toastBread' and the following error: Incompatible types in assignement</p> <p>Which is logical, since I have defined the type of obj as GenericObject. I want to be able to use methods from GenericObject in GenericView, and methods from SpecializedObject in SpecializedView. Is there a way to precise that obj has to be a GenericObject in GenericView to be handled, and has to be a SpecializedObject to be dealt with in SpecializedView, without adding a property? How would you do that?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload