• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

question from mughals book

 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the answer given is 4...but why not 6?
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya maybe the because tthe first error is asked......crazy book :|
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers Ikpefua, it's all clear now
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
I like tacos! And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic