deck
is my superclass and playingCardDeck
is my subclass of deck.
i found that i can i instantiation my superclass by using my subclass which confuse me a lot.
Can you tell me about this.Which init
method will be used and any other features about this.thanks in advance.
#import "XYZViewController.h"
#import "PlayingCardDeck.h"
@interface XYZViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipLabel;
@property (nonatomic) NSUInteger flipCount;
@property (nonatomic) Deck *deck;
@end
@implementation XYZViewController
- (Deck *)deck
{
if (!_deck) {
_deck=[self createDeck];
}
return _deck;
}
- (Deck *)createDeck
{
return [[PlayingCardDeck alloc]init];
}
This shouldn't be surprising at all. This is quite normal OOP (it's formally called the Liskov substitution principle). An object of type Animal
can accept an object of type Dog
. But you can only call Animal
methods on it.
In your example, the init
for PlayingCardDeck
will be executed. Anyone who accesses deck
will only be able to call methods that are defined on Deck
, but the implementation will be the one provided by PlayingCardDeck
.