Calling different static methods based on real type of argument 1Z0-815 Book (Second Edition, August 1, 2020 Build 2.1)

Moderator: admin

Post Reply
JJJ2020
Posts: 20
Joined: Mon Sep 21, 2020 8:41 am
Contact:

Calling different static methods based on real type of argument 1Z0-815 Book (Second Edition, August 1, 2020 Build 2.1)

Post by JJJ2020 »

Hi:

I found the programming exercises at the end of each of the first 12 chapters to be something that sets this book apart from competing books that seem to think that User's End Goal and Ambitions in life end after successfully passing a certification exam. Of course, the reward for that, at best, is a job in which you'd better know how to program. This book seems never to forget either that the reader wants to clear an exam, or that they should be a decent programmer if they get hired.

Anyway, in light of that, except for a couple of gripes about the exercises not following chapter sequence after rewrite, all of the chapter exercises were of an appropriate difficulty. One or two I was unsure of whether my solution was the one that the authors intended, but not enough to post a question.

In Chapter 13 we find problem 5 that asks us to call different static methods based on the actual type of an object reference passed in.
First of all, I think that would not be very common practice, but might be a great "Did you understand this whole chapter's material" type of question.

So...my first thought was to have an if-else-tree using instanceof -- that will work if one hard-codes every child type that one expects to handle, and can call a default otherwise. It will not automatically work if someone adds new child classes and calls with them.

Then even tho reflection hasn't really been covered, I thought "What would I do at a whiteboard with a gun to my head?" and tried to use reflection.
I had the same issue, I found myself having to hard-code the fully-qualified-classnames of the different classes, leading me to think there's a better answer.

I figured we were supposed to use generics then. However, my attempts at using generics just merely called the base class static method, probably due to type erasure?

Now I am wondering -- in which way were we expected to solve this? I have two ways that work but require hard-coding all possibilities, so it isn't like I had nothing. I am ready to slap my forehead when I see how easy this one is. Also, I feel like I normally wouldn't be doing this in real life coding.

Cheers,
Jesse

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Calling different static methods based on real type of argument 1Z0-815 Book (Second Edition, August 1, 2020 Build 2

Post by admin »

You are right hard-coding can't be avoided here and that's the point. This is one of those problems where the student needs to call out that this is a bad design. If calories depends on the kind of Pie, getCalories must be polymorphic and it doesn't make sense for getCalories to be static.
May be this problem needs a little more information to nudge the student in that direction.
Paul.
If you like our products and services, please help us by posting your review here.

JJJ2020
Posts: 20
Joined: Mon Sep 21, 2020 8:41 am
Contact:

Re: Calling different static methods based on real type of argument 1Z0-815 Book (Second Edition, August 1, 2020 Build 2

Post by JJJ2020 »

A-HA! I can even see doing this. Creating an instance method that is "Instance in name only" but returns the static value for the class.
In other words, relies on polymorphism and vtables to call a method that then essentially just returns a static?
The only disadvantage is that it can't be called without a reference to an object.

Cheers.

Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests