1

I have 3 classes A,B and c as follows

A.java

class A
{
    protected A(){
        System.out.println("A");
    }
    void show()
    {
        System.out.println("showA");
    }
}

B.java

class B extends A
{
    B(){
        System.out.println("B");
    }
    void show()
    {
        System.out.println("showB");
    }
}

C.java

class C extends B
{
    C(){
        System.out.println("C");
    }
    void show()
    {
        System.out.println("showC");
    }
    public static void main(String... args)
    {
        A a= (B)new C();
        a.show();
    }
}

When executed gives the output

D:\java\rmi\Hello>javac C.java

D:\java\rmi\Hello>java C
A
B
C
showC

I know a superclass cannot be casted to a subclass.But in the output why is it executing the C class method (show) when there is a cast to the super class B?

A a= (B)new C();

And if this is right then what is it that is getting casted to B? I mean here new C() would call the C constructor and hence the respective outputs but what is the difference between new C().show(); and (B)new C().show(); what is getting casted here?

Galaxin
  • 474
  • 2
  • 9
  • 21

4 Answers4

1

Casting an object does not change its type.

(B) new C();

will still create an object of type C, no matter what you cast it to. But if you only create a B it will of course only ever be a B which is why C’s constructor is not called when you execute

(C) new B();
Bombe
  • 81,643
  • 20
  • 123
  • 127
  • ok but my question is about the show method call and not the respective constructors.Are the two calls new C().show(); and (B)new C().show() absolutely same with no difference at all? – Galaxin Feb 03 '12 at 12:23
  • It’s not really about `show()` because it is never called. The exception occurs in the line above the call to `show`. However, `((B) new C()).show()` and `new C().show()` are indeed the very same method. – Bombe Feb 03 '12 at 12:32
1

The error "B cannot be cast to C at C.main" means that the superclass B is casted by C which is a subclass.(Just read it again and again and you will understand it..)

You can cast in lower element in the hierarchy with the upper element but not vice-verse..
Hope you got it :)

Krunal
  • 7,048
  • 1
  • 18
  • 19
0

Class B does not extend C, so it has no knowledge of it and connot be casted to C. With java, you can down cast but not upcast.

OK:

A a= (B)new C();

-> C inherits from B, so cast is possible

Not OK

A a= (C)new B();

-> B does not inherit from C, so it cannot be cast to it

see also: Downcasting in Java

Edit: Please consider to edit your question, as most users tend to correct an error first. (Remove the error and break it down to your original question)

"what is the difference between new C().show(); and (B)new C().show();" This is called 'polymorphism'. there is no difference between the two calls, as java will always execute the method of the lowest level of the hierarchy.

For example:

class Bird{
 public void fly(){
   System.out.println("I am flying");
}}

class Duck extends Bird{
 public void fly(){
   System.out.println("I can not fly");
}}

class Test{
 public static void main(String[] args){
  Bird[] birds = {new Bird(), new Duck()};
  for (Bird b: birds){
   b.fly();
  }
}

This would output:

I am flying
I cannot fly
Community
  • 1
  • 1
steffinchen
  • 497
  • 4
  • 12
  • Thanks for the explanation, but my question is about the call to the show() that is present in all classes,and the C's show is executing inspite of A a= (B)new C();.so there is absolutely no difference between new C().show(); and (B)new C().show(); ? – Galaxin Feb 03 '12 at 12:33
  • java always takes the method at the lowest level, in your case that is class 'C', even if casted. so yes, C().show(); and (B)new C().show(); executes the same method. – steffinchen Feb 03 '12 at 12:54
0

When you are creating an object of a class, its superclass objects are created first. So when you says A a = (B)new C(); While creating object of C, its superclass objects are first created. So the object of C can be casted to B. But in the later case while creating the object of B, it would not possible.

Always remember that a subclass object can be casted into superclass, where as a superclass object can not be casted to its subclass, which is done by you in the second case, so it gives you compilation error.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • Thanks for the explanation, but my question is about the call to the show() that is present in all classes,and the C's show is executing inspite of A a= (B)new C();.so there is absolutely no difference between new C().show(); and (B)new C().show(); ? – Galaxin Feb 03 '12 at 12:33
  • The difference between those two is, in the first one new C() can be given to any of the reference of class A,B or C, but in the second case (B)new C() can be given to either the reference of class A or B but not to C. – Chandra Sekhar Feb 03 '12 at 13:04