This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question from mughals book

 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the answer given is 4...but why not 6?
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya maybe the because tthe first error is asked......crazy book :|
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a simple real life rule:

A child can be parent, but a parent can not be child!
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
T Vergilio
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
T Vergilio
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Ikpefua, it's all clear now
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
 
T Vergilio
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
T Vergilio
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic