This week's book giveaway is in the JavaScript forum.
We're giving away four copies of JavaScript Promises Essentials and have Rami Sarieddine on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from mughals book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question from mughals book" Watch "question from mughals book" New topic
Author

question from mughals book

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521


the answer given is 4...but why not 6?


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
ya maybe the because tthe first error is asked......crazy book :|
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Raju Champaklal wrote:

the answer given is 4...but why not 6?

see the question he's is asking about "first line that will cause compilation".
but actually error will rise on both label 4 and 6 on compile time.


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

there is a simple real life rule:

A child can be parent, but a parent can not be child!
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
I don't understand.

line 3:



now both a and b refer to an object of type MySubclass.

line 4:



reference b now refers to the same object which reference a refers to (i.e. the MySubclass instance).

or not?

Would I need a cast, because the compiler doesn't look at the object, it looks at the variable type?


OCA, OCP Java 6
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Chandella Montero wrote:Would I need a cast, because the compiler doesn't look at the object, it looks at the variable type?


Hello Chandella...Yes you got it right, the compiler checks the reference variable type so if you cast the code compiles as follows:



Regards

Ikpefua


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
Cheers Ikpefua, it's all clear now
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

@Chandella the cast is ONLY to resolve the problem in line 11 of the code, because line 13 has the SAME problem(even worse)...Now here is a complete version of the program MODIFIED to make it compile and execute without problems:

You can try it out yourself and you will see that this program as-it-stands compiles and runs without exceptions

Regards

Ikpefua
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
That's a good way of preventing line 11 from executing, using the recommended if + instanceof test.

When I first did the exercise, I marked (f) too. Naïve...
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

@Chandella...thats Java for you...It took me a long time to understand the concepts of inheritance-polymorphism-reference-variable-casting, you need to practice with a lot of coding, that is write lots of small test programs and write down your findings.

Cheers

Ikpefua.
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
Thanks Ikpefua, you keep me motivated. I'm working through Mughal's book now and doing all the exercises: the programming ones are quite fun to do, if only the exam were like that (i.e. allowed you to make silly mistakes, then go and correct them, then try to compile again). Oh well, at least I am learning in the process. As you said, it takes time. That's why it's worth something in the end. If it wasn't so hard, do you think we would want it that much?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question from mughals book