|
Hi! Can anyone help me validate my solution in checkpoint 7? I feel like it's already close to being solved although it's not yet 100% solved. I'm not sure why I'm also encountering the "cannot override mutable property with read-only property 'isTame' error even though I've stated that I want to override it. Lastly, how do I call these classes? Do I simply write var animal = Animal() then print my var? var corgi = Corgi() print(corgi)? Because I tried it but it doesn't seem to work even though I have passed how many legs they should have inside my class already. Here's my solution
|
|
Hi! I think you are a bit overusing
if you want Lion class to be false upon init you can do as follows:
|
|
So I really have to put it inside init if I want to override it? I can't just set it like a normal variable and just override the variable afterwards without using initializers? Also, I've searched convenience init up since it's the first time that I'm encountering that term and found that you have to place the keyword convenience before your initializers. However, I didn't see you put any convenience inits. Is there any reason as to why? Lastly, is there a huge difference between normal inits and convenience inits? |
SPONSORED Transform your career with the iOS Lead Essentials. This Black Friday, unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a free crash course. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
All classes need an Convenience initializers are additional initializers that provide alternate ways of creating your class. They have to call the designated initializer. An example:
So this allows you to initialize an instance of the
|
|
in rect1, does swift also know what our height and width would be? Or it doesn't need to know which is which? |
|
Height and width are not properties of the |
|
Maybe these examples will provide more clarity for using override.
|
|
I did some additional reading and tried to search for more information and here's my current understanding of the different concepts. Kindly help verify if my understanding is now correct. Initializers - creating something from scratch (we define the different properties and set the values for them). Convenience Initializers - we have an existing template and we just update whatever we want to update. Overriding - process of editing the properties depending on what we want. Initializers - We set an init and define the different properties that we want.
Convenience Initializers
Overriding We simply add the keyword override and edit the values of the initializers to match whatever our class should have
|
|
Exactly, in more academic language it means: The chief task of an initializer is to ensure that all properties have an intial value, making the instance well-formed as it comes into existence: and initializer may have other tasks to perform that are essential to the inital state and integrity of the instance. A class, however, may have a superclass, which may have properties and initializers of its own. Thus we must somehow ensure that when a subclass is initialized, its superclass’s properties are initialized and the tasks of its initializers are performed in good order, in addition to those of the subclass itself. Designated initializer A class initializer, by default, is a designated initializer. A class can be instantiated only through a call to one of its designated initializers. A designated initializer must see to it that all stored properties are initialized. Convenience initializer A convenience initializer is marked with the keyword convenience. A convenience initializer is not how a class is instantiated; it is merely a façade for a designated initializer. A convenience initializer is a delegating initializer; it must contain the phrase self.init(...), which must call a designated initializer in the same class. Here are some examples. This class has no stored properties, so it has an implicit init() designated initializer:
This class’s stored properties have default values, so it has an implicit init() designated initializer too:
This class’s stored properties have default values, but it has no implicit init() initializer because it has an explicit designated initializer:
This class’s stored properties have default values, and it has an explicit initializer, but it also has an implicit init() initializer because its explicit initializer is a convenience initializer. Moreover, the implicit init() initializer is a designated initializer, so the convenience initializer can delegate to it:
Main thing for convenience init it must call DESIGNATED initializer.
Not really so. Overriding It is also permitted for a subclass to redefine a method inherited from its superclass. For example, perhaps some dogs bark differently from other dogs. We might have a class NoisyDog, for instance, that is a subclass of Dog. Dog declares bark, but NoisyDog also declares bark, and defines it differently from how Dog defines it. This is called overriding. You cannot override properties as such using this keyword, you are overriding methods aka functions that have the same name and using the same parameters. init() is also a method where you provide all your properties with initial values. |
|
As for these questions:
you use override when you have exactly the same method name which takes the same parameters in your superclass. Here I just used init with different parameters in Persian class than those in Cat class, so they are considered different and you don't need to use override keyword. Once again to make it maybe more clear. You can use this in Persian class.
There numerous way you can use intializers. All depends on your needs. for that particular assignment you don't need to go so deep. Just more or less to understand how initialization of classes happens. It comes with time and pratice. When I started, I couldn't undertand that at all :)))) and I have not CS background. |
|
I see. I think I'm getting a bit more understanding about it now. To summarize, initializers allows us to pass in a value as parameter when creating an instance of a class.
The override keyword is only used when we want to edit a function name that has the exact same name with the parent class' function. Otherwise, we don't need to use the override keyword and can create a new function name that doesn't exist in the parent class. Example: Animal is a parent class with a function legs. Dog is a class that inherits animal but speak() method/function does not exist in the parent class (Animal). Therefore, the keyword "override" is not necessary in our speak method/function. However, Poodle class will inherit the Dog class and Poodle also needs to use speak(). Therefore, we now need to add an override keyword in the function. Kindly let me know if my understanding is now correct. Thank you! |
|
This is correct!
but if you want both to be accessible you change your code like so for convenience:
|
|
SPONSORED Transform your career with the iOS Lead Essentials. This Black Friday, unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a free crash course.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.