Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Class Inheritance Overriding Properties

NIKOLA RUSEV
NIKOLA RUSEV
5,293 Points

inheretence and init (override)

hi, I just tried to put some statements about this chapter to keep it simple for my self. but i'm not sure every statements below is correct. i ask you kindly for someone who is pro to check this statements let me know which statement is incorrect. (sorry for my letter mistake) tnx in advance

1 IF WE WANT TO ADD NEW PROPERTY TO SUBCLASS IS VERY IMPORTANT IS THAT NEW PROPERTY HAS THE DEFAULT VALUE OR NOT(just declaration of type)

  • IF IT HAS A DEFAULT VALUE WE JUST ADD NEW PROPERTY WITH VALUE (but outside of init). the rules say that we must first provide initial value for new property and after that we can provide initial value for superclass property. THIS HAPPEND CUZ WHEN WE ACTUALY CALL INSTANCE OF SUBCLASS WE CALL A INSTANCE OF SUPERCLASS and must be sure that superclass property has the initial value. BECAUSE OUR NEW PROPERTY HAS A DEFAULT VALUE we dont have need to provide PARAMETAR PLACE IN INIT METHOD, AND BECOUSE WE DONT CHANGE INIT METHOD IN PARAMTARS WE WRITE OVERRIDE BEFORE INIT. Now the rule is sytisfy and new properties has initial value first. AFTER THAT WE CALL WITH SUPER.INIT INIT METHOD FROM THE SUPERCLASS AND SET INITIAL VALUE FOR SUPERCLASS PROPERTIES (which are without defoult values. HERE OUR SUPER CLASS IS READY FOR USE. BUT IF WE WANT TO CHANGE SOME PROPERTY (with default value) WHICH WE INHERET FROM SUPERCLASS WE DO THAT AFTHER ALL PROPERTY HAS A INITIAL VALUES. AND OUR SUBBCLASS IS READY FOR USE.

  • IF IT HASNT A DEFAULT VALUE WE MUST PUT A PARAMETAR FOR THAT NEW PROPERTY AND IN THE BODY OF INIT FIRST WE MUST PROVIDE A INITIAL VALUE FOR NEW PROPERTY. becouse we are forced to MAKE CHANGE IN PARAMETARS OF INIT, this INIt is brand new and dosnt need to be OVERRIDE cuz its not match with init in superclass. WHEN WE PROVIDE INITIAL VALUE FOR NEW PROPERTY IN THE BODY OF THIS BRAND NEW INIT, WE SHULD THEN PROVIDE INITIAL VALUE FOR PROPERTIES THAT WE INHERET FROM SUPER CLASS BUT DONT HAVE THE default values. WE USE SUPER.INIT IN TO THE BODY OF THIS BRAND NEW INIT AFTER VALUES FOR NEW PROPERTIES AND GIVE THOSE PROPERTIES INITIAL VALUES. AGAIN IF WE NEED TO CHANGE SOME PROPERTIES WITH THE DEFAULT VALUES THAT WE INHERET FROM SUPERCLASS WE DO THAT AFTER ALL PROPERTIES HAS INITIAL VALUES.

2 IF WE ADD NEW PROPERTY WITH DEFAULT VALUES BUT WE DONT MAKE CHANGHE IN INHERETED PROPERTIES WE DONT NEED TO OVERRIDE INIT CUZ ALL PROPERTIES NOW HAS THE INITIAL VALUES AND OUR SUBCLASS IS READY TO USE. (no matter does inhereted properties are with or without default values)

3 IF WE ADD NEW PROPERTY WITHOUT DEFAULT VALUS (only declaration of type) AND NOT MAKE CHANGE IN INHERETED PROPERTIES. WE SHOULD MAKE BRAND NEW INIT TO PUT PARAMETAR IN INIT (for new and inhereted properties) and after again follow the rules: first to provide initial value for new property and after with super.init provide value for INHERETED PROPERTIS WITHOUT DEFAULT VALUES.

4 IF WE WANT TO MAKE CHANGE IN INHERETED PROPERTIES AND WE DONT HAVE NEW PROPERTY WE JUST OVERRIDE INIT AND PROVIDE INITIAL VALUE FOR PROPERTIES WITHOUT DEFAULT VALUES (super.init) and with (self.x) modify the properties (because modifing of properties can be only during the procces of initialization)

5 IF OUR SUPERCLASS HAVE ONLY PROPERTIES WITH DEFAULT VALUES AND SUBCLASS DOSNT HAVE NEW PROPERTY. IF WE WANT TO MAKE CHANGE IN SUBCLASS WE WRITE OVERRIDE INIT(){ SELF.X = CHANGE)

6 IF OUR SUPERCLASS HAVE ONLY PROPERTIES WITH DEFAULT VALUES AND SUBCLASS NEW PROPERTY WITHOUT DEFAULT VALUES. WE WRITE JUST A BRAND NEW INIT(PARAMETAR FOR NEW PROPERTY){INITIAL VALUE FOR NEW PROPERTY}

7 IF OUR SUPERCLASS HAVE ONLY PROPERTIES WITH DEFAULT VALUES AND SUBCLASS NEW PROPERTY WITH DEFAULT VALUES. WE JUST ADD NEW PROPERTY AND ASSIGN VALUE. NO NEED OF OVERRIDE AND INIT.