• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Downcasting and upcasting Objects

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Komal Arora
Ranch Hand
Posts: 91
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, please help me with the above type of problems as well..
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone please help
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  •  
    Trivikram Kamat
    Ranch Hand
    Posts: 155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    mohitkumar gupta wrote:
    Anyone please help


    PatienceIsAVirtue
     
    Mohit G Gupta
    Ranch Hand
    Posts: 634
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 155
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 634
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 9528
    33
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    Mohit G Gupta
    Ranch Hand
    Posts: 634
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 423
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Ankit Garg
    Sheriff
    Posts: 9528
    33
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 423
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry Ankit.
     
    Mohit G Gupta
    Ranch Hand
    Posts: 634
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 9528
    33
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 634
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator



    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
    Posts: 9528
    33
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 634
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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;
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic