1

This is a simple code that I have written:

class c1
{
    int x = 10;
}
class c2 extends c1
{
    int x = 20;
}
class c3
{
    public static void main(String [] args)
    {
        c1 a = new c2(); 
        System.out.println(a.x); //This works and output is 10.
        System.out.println((c2)a.x); //Error is here
    }
}

And the error says,

incompatible type, int cannot be converted to c2

I am sure the code is correct. Since the type of reference a is converted to c2 (in the statement where error is being displayed), 20 should be displayed.

But, typecasting is not happening. Why?

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Krishna
  • 13
  • 3

5 Answers5

6

Should be ((c2) a).x

You need to have an extra parenthesis to make sense. Because the compiler thinking that you are trying to case whole thing to c2.

System.out.println(((c2) a).x);
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

It's a matter of operator precedence (cast versus dot), except that unfortunately the JLS doesn't consider these to be operators and so doesn't publish their precedence in a table. Here's the precedence:

  • The new operator has highest precedence: new A().foo(); means (new A()).foo();
  • The dot operator has higher precedence than the cast operator: (double)a.x means (double)(a.x)

These are not known as "operators" by Java programmers, but in other languages they are. So for readers coming from other languages, this can make sense.

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
0

currently, you are trying to convert a.x to C2 which is an integer. as you need to convert the c1 to c2 try

   System.out.println((c2)a)).x;
Sunil Soni
  • 443
  • 3
  • 18
0

You're not casting c1 to c2 with (c2)a.x, you're casting the integer x to c2, hence the error.

Kim G Pham
  • 145
  • 6
0

At very first, I would like to correct you at this line..

I am sure the code is correct.

No, the code is not correct.

I've tried explaining your code step by step, with concepts as comments in code.

public static void main(String [] args)
{
    c1 a; // this is just a reference of Type 'c1', holding nothing.
    a = new c2(); // assigned new Object of Type 'c2', perfectly fine, no issues with polymorphism.
    /* but here,
     c2 has value of 'x' as 20;
     but when if you use the reference of 'a' (which is of type c1), i.e. 'c1.x' then JVM follows the reference.
     Meaning to say is, it will go to class 'c1' and print it's value of 'x' which is 10 & you are getting the same.

    */
    System.out.println((long)a.x); //This works and output is 10.
    /*
     your code, (c2)a.x
     Now, Problem starts here,
     'a.x' is of type 'int', and you are casting this 'int' to type 'c2',
     you will have to convert the reference of 'a' to 'c2'
     (i.e 'a' of type 'c1' to 'a' of type 'c2', not 'a.x' to type of 'c2').
     So the solution would be, ((c2)a) <---- this is now of type 'c2', now access it's member 'x', it will show you value 20.


     */

    System.out.println((c2)a.x); //Error is here
}

You should read these two answers related to casting in java.

1) https://stackoverflow.com/a/30776990

2) https://stackoverflow.com/a/1302612

miiiii
  • 1,580
  • 1
  • 16
  • 29