• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Java exam question, casting

Here's a test question found on http://www.geocities.com/skmajji/Main.html
The anwer is a LIE! :-)  Why is the correct answer D.  Did the
program set the base clase object reference to the extended class?


:    class Test
2:    {
3:       static void show()
4:       {
5:          System.out.println("Show method in Test class");
6:       }
7:    }
9:    public class Q2 extends Test
10:   {
11:      static void show()
12:      {
13:          System.out.println("Show method in Q2 class");
14:      }
15:      public static void main(String[] args)
16:      {
17:           Test t = new Test();
18:           t.show();
19:           Q2 q = new Q2();
20:           q.show();
22:           t = q;
23:           t.show();
25:           q = t;
26:           q.show();
27:      }
28:  }

A) prints "Show method in Test class"
          "Show method in Q2 class"
          "Show method in Q2 class"
          "Show method in Q2 class"

B) prints "Show method in Test class"
          "Show method in Q2 class"
          "Show method in Test class"
          "Show method in Test class"

C) prints "Show method in Test class"
          "Show method in Q2 class"
          "Show method in Test class"
          "Show method in Q2 class"

D) Compilation error.
  • 6
  • 4
  • 2
  • +1
1 Solution
zzynxSoftware engineerCommented:
>> Why is the correct answer D.
If it is, I think the compilation error you get will explain it ;°)
zzynxSoftware engineerCommented:
>> q = t;
Incompatible types
found   : Test
required: Q2

>> Why is the correct answer D
It's not because a Mercedes is a Car, that every Car is a Mercedes
Mercedes = Q2, Car = Test
>>The anwer is a LIE! :-)

Says who?! Have a look in any IDE like Eclipse and it will tell you all you need to know.

Try to run it and then change  q = t; to q = (Q2) t; and try again
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

hank1Author Commented:
I guess I assume when you did this

t = q;

they are now the same thing

later when

q = t

isn't that just saying Apples = Apples?
hank1Author Commented:
        if (t instanceof Test) System.out.println("T is a TEST object");
           if (t instanceof Q2) System.out.println("T is a Q2 object");

           t = q;
    if (t instanceof Test) System.out.println("after T is a TEST object");
           if (t instanceof Q2) System.out.println("after T is a Q2 object");

AFter the assignment t is a q2.
and this is true
if (t == q) System.out.println("Objects are the same!");
javac error:  (buy why ... they are the same?
Q2.java:32: incompatible types
found   : Test
required: Q2
          q = t;
1 error
They aren't the same...

Image "Test" is called "Humans" and "Q2" is called "Men"

Men are humans, so you can set a Human variable to contain a Man

Not all Humans are Men though, so you cannot store a Human object in a Man variable...

What if it were an instance of a woman?  Things would go horribly wrong (seen the Fly?) ;-)

Hope this goes some way to explain it...

Everyone else deserves the points for this Q more than me, as I am just repeating what they have said...

>> Image "Test" is called "Humans" and "Q2" is called "Men"

IMAGINE "Test" is called "Humans" and "Q2" is called "Men"
hank1Author Commented:
OK a different question which will help pound this past the rock here
in my skull.

Why is this true?

if (t == q) System.out.println("Objects are the same!");

that just means that both variables contain exactly the same object...

so assuming you have done:

  t = q ;

then both t and q contain the same object (an instance of Q2)
It's like you can do:

    Object o = new String( "Woot!" ) ;

but you cannot do:

    String o = new Object() ;

as String extends Object, so is considered more specific and so cannot contain the more general "Object" instance...

that's probably a good way of thining of it...

Imagine you have:

class Chest
    // get the weight of this object...
    public int getMass() ;

class Torso extends Chest
    // return the number of limbs
    public int numberofLimbs() ;

class EntireBody extends Torso
    // get the eyecolor
    public Color getEyeColor() ;

If you have:

Chest c = new Chest() ;

that's fine...


EntireBody b = new EntireBody() ;

is fine too...

and you can do:

c = b ;

as c is more general (and you can call getMass() on an EntireBody class)

but you can't do:

b = c ;

as c is a Chest object, so "getEyeColor()" makes no sense (and cannot be called) on b.

Hmmm...actually...that's kinda badly put too ;-)

hope it makes a bit of sense ;-)

hank1Author Commented:
So why the beef with ( since previously they did a t = q )
(ps - would this also schedule the now defunct t object
for garbage collection?)

q = t;  ?  They are the same.  Right?

The compiler want

q = (Q2)t;  Why?

ps - thanks for suffering a knuckle head.  I still don't get this.  :-)
>> q = t;  ?  They are the same.  Right?

At run time, yes...  the compiler isn't good enough to pick that up though...  I don't think any compiler is good enough...

Syntactically, they could be incompatable objects...  it just so happens that at runtime, you make them the same object...

>> The compiler want
>> q = (Q2)t;  Why?

Because by casting it, you are telling the compiler to shut up, that you know what you are doing, and to trust you that you won't do anything bizarre...

if t is incompatable with q, you will then get a ClassCastException at runtime
>> ps - thanks for suffering a knuckle head.  I still don't get this.  :-)

No worries...  it's kinda hard to explain ;-)  but it will click, believe me :-D
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now