0Pricing
Objective-C iOS Development for Legacy & Enterprise Apps · Lezione

Ereditarietà e override dei metodi

Costruisca gerarchie di classi in Objective-C con il subclassing, esegua l'override dei metodi ereditati e richiami la superclasse usando super.

Ereditarietà e override dei metodi è una lezione Objective-C iOS Development for Legacy & Enterprise Apps gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Objective-C iOS Development for Legacy & Enterprise Apps, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Objective-C iOS Development for Legacy & Enterprise Apps include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

What is Inheritance?

Inheritance lets a class reuse and extend another class. The new subclass gets all the methods and properties of its superclass, then adds or changes behavior.

Declaring a Subclass

The colon syntax in the interface declares the superclass. Here Dog inherits from Animal.

@interface Dog : Animal
- (void)fetch;
@end

Inherited Members

A subclass automatically has the superclass's public methods and properties — no need to redeclare them.

Dog *d = [[Dog alloc] init];
[d eat];   // inherited from Animal
[d fetch]; // defined on Dog

Overriding a Method

To change inherited behavior, redeclare a method with the same name in the subclass. This is overriding.

@implementation Dog
- (void)makeSound {
  NSLog(@"Woof!");
}
@end

Calling super

Use super to invoke the superclass version, extending rather than replacing its behavior.

- (void)makeSound {
  [super makeSound];
  NSLog(@"...and wags tail");
}

Overriding init

Custom initializers almost always call [super init] first to set up inherited state, then add their own.

- (instancetype)init {
  self = [super init];
  if (self) {
    _legs = 4;
  }
  return self;
}

Polymorphism

A superclass-typed variable can hold any subclass instance. The correct overridden method runs at runtime — this is polymorphism.

Animal *pet = [[Dog alloc] init];
[pet makeSound]; // prints Woof!

The instancetype Return

Initializers return instancetype, which adapts to the actual class being created — safer than hardcoding the type.

+ (instancetype)dogWithName:(NSString *)name;

Checking Class at Runtime

Objective-C is dynamic. You can ask an object about its class.

if ([pet isKindOfClass:[Dog class]]) {
  NSLog(@"It is a dog");
}

NSObject the Root

Almost every class ultimately inherits from NSObject, which provides core behavior like alloc, init, isEqual:, and memory management hooks.

Designing Hierarchies

Favor shallow hierarchies. Use inheritance for genuine is-a relationships; for shared behavior across unrelated classes, prefer protocols and categories instead.

Quick Check

Test your inheritance knowledge.

Recap

You learned inheritance in Objective-C:

  • Subclass with the : Superclass syntax
  • Inherited methods and properties come for free
  • Override methods to change behavior
  • Use super to extend the parent version
  • Polymorphism picks the right method at runtime

Inheritance and polymorphism are pillars of OOP in Objective-C.

Domande Frequenti

La lezione «Ereditarietà e override dei metodi» è gratuita?

Sì — il testo completo di «Ereditarietà e override dei metodi» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Objective-C iOS Development for Legacy & Enterprise Apps, passa a CoddyKit PRO. Il corso Objective-C iOS Development for Legacy & Enterprise Apps include 4 lezioni in totale.

Cosa imparerò in «Ereditarietà e override dei metodi»?

Costruisca gerarchie di classi in Objective-C con il subclassing, esegua l'override dei metodi ereditati e richiami la superclasse usando super. Eserciti Objective-C iOS Development for Legacy & Enterprise Apps con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Objective-C iOS Development for Legacy & Enterprise Apps?

Non è richiesta alcuna esperienza precedente. Objective-C iOS Development for Legacy & Enterprise Apps su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Ereditarietà e override dei metodi»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Objective-C iOS Development for Legacy & Enterprise Apps?

Sì. Ogni lezione Objective-C iOS Development for Legacy & Enterprise Apps include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Classi, oggetti e metodi
  2. Proprietà e variabili di istanza
  3. Protocolli e categorie
  4. Ereditarietà e override dei metodi
← Torna a Objective-C iOS Development for Legacy & Enterprise Apps