Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Downcasting and upcasting Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Downcasting and upcasting Objects" Watch "Downcasting and upcasting Objects" New topic
Author

Downcasting and upcasting Objects

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Exam lab


i fail to understand the above problem
shouldn't there be a null pointer exception on line 2


i have my exam on 15thoct

please tell me some technique to solve such type of questions.
everytime i try them,i fail to get it correct


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Komal Arora
Ranch Hand

Joined: Sep 30, 2010
Posts: 91

Yeah, please help me with the above type of problems as well..


OCPJP
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Anyone please help
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
mohitkumar gupta wrote:
shouldn't there be a null pointer exception on line 2


In Line2 you assigned value present m to c
It's simple assignment, and you're not trying to access (say) instance method of the object.

It'll throw exception, if you say b.funcName(); after line2, where funcName() is instance method.

NullPointerException is thrown in following case:
  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.


  • OCPJP6
    Trivikram Kamat
    Ranch Hand

    Joined: Sep 26, 2010
    Posts: 155
    mohitkumar gupta wrote:
    Anyone please help


    PatienceIsAVirtue
    Mohit G Gupta
    Ranch Hand

    Joined: May 18, 2010
    Posts: 634

    what about


    please tell me some technique to solve such type of questions.
    everytime i try them,i fail to get it correct
    Trivikram Kamat
    Ranch Hand

    Joined: Sep 26, 2010
    Posts: 155
    I don't know whether any technique exists.
    If you understand the Java language, you don't require any "Technique" as such.

    One way to understand is to code more in Java (without using IDE).
    The more mistakes you make, the better you will understand.

    You can try solving problems in Java on online coding sites such as SPOJ, TopCoder, CodeChef, Project Euler etc
    Or else, you can contribute to some Open Source Projects in Java
    Mohit G Gupta
    Ranch Hand

    Joined: May 18, 2010
    Posts: 634

    ok,can anybody tell me what exactly each of the following line means:





    i would be grateful if someone explains this to me
    i know what's upcasting and downcasting is
    please tell me what exactly is happening in each of the line.
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9291
        
      17

    One thing you need to know is that a cast between unrelated class and interface is allowed. The cast may fail at runtime but at compile time its allowed. Look at this casting tutorial for more info. Other casts should be simple to understand...


    SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
    Mohit G Gupta
    Ranch Hand

    Joined: May 18, 2010
    Posts: 634

    ANkit,i read the article you told to refer but still i am facing the same problem while solving these questions
    What to do ?

    can you refer any other article
    Harikrishna Gorrepati
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 422
    This is an excellent tutorial.
    Hi Ankit, In your tutorial at http://www.javaclubindia.com/core-java/difference-between-object-and-reference-in-java at second diagram, you represented name="Eric" string within Heap which I feel may not correct. What I am trying to say is when we say, String name="Eric", Eric should be created in string constant/memory pool and when we say String name = new String("Eric"), Eric should be created on Heap know ? Please advice if I am wrong.


    OCPJP 6.0-81% | Preparing for OCWCD
    http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9291
        
      17

    Harikrishna lets keep the discussion in this topic to mohit's problem. If you have any doubt start a new topic. Mohit, what exactly are you facing problem with. You've shown about 6 statements which you don't understand. Can you tell us what you think is happening in those statements, and then we'll tell you if you are right or wrong...
    Harikrishna Gorrepati
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 422
    Sorry Ankit.
    Mohit G Gupta
    Ranch Hand

    Joined: May 18, 2010
    Posts: 634

    Ankitwhat exactly are you facing problem with. You've shown about 6 statements which you don't understand. Can you tell us what you think is happening in those statements, and then we'll tell you if you are right or wrong...,

    i am no able to understand any of the lines.i am posting the first 2 initally


    m is Interface that is implemeted by b
    then,its cast to c
    what does this casting exactly means
    we are casting from Supertype to subtype and then assiging the cast to another subtype that is supertype of resultant cast
    in short,
    b=c;
    shouldn't the cast be same as that of reference

    eg.

    am i right ?

    --------------------------------------------------------------------------------


    now here b refers to c from above
    we are casting b to Min(Super type to subtype)
    ie c To Min and then assigning that to m
    ok this is correct as it same as the Dog example
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9291
        
      17


    Here m is first downcasted to type C and then implicitly upcasted to type B.

    The cast here is not required, if you omit it there will be an automatic upcasting.

    This cast falls under the category that I was referring to. Type A and Min are not related still this cast compiles fine as a cast between unrelated class and interface is allowed. Now try to figure out the rest of them...
    Mohit G Gupta
    Ranch Hand

    Joined: May 18, 2010
    Posts: 634




    Here m is first downcasted to type C and then implicitly upcasted to type B.


    How would i know that m can be downcasted to C ?

    ==============================================


    answer:ClasscastException would occur as m would be upcasted to A

    ===============================================




    a2 is of A type that is superclass Of B,a2 would be upcasted to B
    answer:would compile and run

    ==================================================


    b2 would be upcasted to C as C isa B and then b2 would be upcasted to m as C isa Min(b isa Min)
    answer:would compile and run


    ======================================



    answer:would compile and run



    would the previous statement have any effect on it ?

    ==============================

    Is my all answers right ?

    Ankit ,thanks for help
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9291
        
      17

    null can be cast to any type. So there will be no NullPointerException or ClassCastException when you cast null values.

    How would i know that m can be downcasted to C ?

    You have to see the actual object pointed by m. Since m is null at the line b=(C)m; and null value can be casted to any type, the cast will be successful.

    A a2=(A)m;
    answer:ClasscastException would occur as m would be upcasted to A

    Again m is null so the cast to A will be successful at runtime.

    B b2=(B)a2;
    a2 is of A type that is superclass Of B,a2 would be upcasted to B
    answer:would compile and run

    a2 would be downcasted not upcasted to B. Again a2 is null so the cast will be successful at runtime.

    Now try to see if your other answers are right using similar logic that I've used here...
    Mohit G Gupta
    Ranch Hand

    Joined: May 18, 2010
    Posts: 634


    A a2=(A)m;
    answer:ClasscastException would occur as m would be upcasted to A


    Again m is null so the cast to A will be successful at runtime.


    what if m had pointed to an object ?


    e.g.

    Min m=a;
    and
    Min m=b;
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Downcasting and upcasting Objects
     
    Similar Threads
    Casting problem
    Problem With Class Hierarchy
    examlab - 1 [reference variable casting]
    can't understand examlab downcasting question..
    ExamLab Question