Stefan Evans wrote:Your understanding looks fine to me.
But just to follow through on some bits you haven't explicitly mentioned:
If we added a Pig to the list - what would it display?
How would it be different if the Animal class and the noise() method were abstract?
Stefan Evans wrote:
How would it be different if the Animal class and the noise() method were abstract?
Junilu Lacar wrote:Mostly, except your original Pig didn't implement its own noise() method. If, as Stefan proposed, Animal and Animal.noise() were both declared as abstract, you wouldn't be able to put a default behavior of noise() in Animal. So, what would happen?
Junilu Lacar wrote:Aside from that, I believe you're ready to move on. You seem to show a good understanding of the workings of the code you wrote.
Junilu Lacar wrote:One more thing I'll add for you to consider. Is the code below legal? Will it fail at compile time or run time? Why or why not?
Junilu Lacar wrote:And one last thing, before I forget. Please, please, avoid using instanceof. Treat it like a red flag, especially if you're not writing code that will go into some kind of general-purpose library that uses a lot of reflection. Polymorphism and instanceof are incompatible with each other.
Paul Clements wrote:
Line 4 – You are creating a second ArrayList called farm which is a cast of fidos i.e. you’re generating an Animal ArrayList from a Dog one. Ok. Haven’t seen that before but in theory I don’t see why not as Dog is a Subclass of Animal i.e. polymorphism says it should be possible to upward cast like this.
Henry Wong wrote:A cast doesn't actually "create" anything. It doesn't "generate" anything either. What that line does is declare a new reference variable (named farm), which is assigned to the same object as the fidos variable.
Given this information, I will let you try to answer Junilu questions again...![]()
Henry
Paul Clements wrote:As I said my understanding is that Pig would need to define noise() as it is a concrete class below an abstract class containing an abstract instance of the noise() method i.e. Pig would have its own noise().
If however, you’re asking what would happen if I didn’t put noise() inside Pig and then ran a.noise() from the ArrayList loop when the list contains a Pig object? Well, without access to my code at the moment I would possibly expect that to give some sort of run-time exception.
The reason being a.noise when referencing a Pig object on the heap would go back up the inheritance tree to Animal, find an abstract method and then think “shit” I don’t have an actual definition of that anywhere. At that point an error would occur…maybe.
Failing that it will simply recognise a lack of noise() in Pig and do nothing i.e. ride any error.
Paul Clements wrote:Therefore farm can only be used to add Dogs. ... Is this correct?
Paul Clements wrote:I’m an experienced coder, but just not with Java. But after only 2 or 3 weeks I feel I am getting to know how it thinks. More than that I’m really enjoying the challenge. Just wish I’d done this 10 years ago. Approaching 50 I’m not sure if employers will take me seriously as an option over a 25 years old grad whose done Java since day one of Uni. But, I’m going to give it a go.
Junilu Lacar wrote:Colder...
Junilu Lacar wrote:
Paul Clements wrote:Therefore farm can only be used to add Dogs. ... Is this correct?
Not quite. Hint: The question was meant to see if you understood the binding process at compile time.
Junilu Lacar wrote:In another recent thread, I mentioned a need for passion for solving problems and coming up with good solutions and a dedication to craftsmanship as something that will make you stand out from the crowd
Paul Clements wrote:Bottom line is if there is nothing to run, then I can't see what would happen, other than nothing. No error, nothing.
Junilu Lacar wrote:
Paul Clements wrote:Bottom line is if there is nothing to run, then I can't see what would happen, other than nothing. No error, nothing.
Well, compilers report errors, too, you know...
Junilu Lacar wrote:
Paul Clements wrote:Therefore farm can only be used to add Dogs. ... Is this correct?
Not quite. Hint: The question was meant to see if you understood the binding process at compile time.
Paul Clements wrote:
Cold/Warm?
Junilu Lacar wrote:
Paul Clements wrote:
Cold/Warm?
Hot, as they say, as an "adult entertainment worker" on payday.![]()
Paul Clements wrote:The reason being you are attempting to create an Animal ArrayList object ref which, even if the compiler allowed it, would only give access to a Dog ArrayList object. Therefore any other form of Animal (other than Dog) put into AnimalList would fail. As such the compiler throws an error because it could never be sure which type of Animal you are going to put into this in effect, restricted/bound to one Animal subclass, AnimalList.
Tobias Bachert wrote:
Paul Clements wrote:What does this line do?
Consider Paul's rocket mass heater. |