• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Upcasting from Class to interface Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

javachamp




output:
classcast Exception


in line 1 upcasting from BMW(sub) to Car(super) allowed
while in line 2 upcasting from Vehicle (sub) to Colorable (super) not allowed

Why is line 2 giving Exception?

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the class Vehicle implement Colorable interface??
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vehicle a = new Car(); //Line 11
Colorable i = (Colorable) a; //Line12

Line 11 is creating a reference of Vehicle and an object of Car.
Why Line 12 is not throwing a ClassCastException - as the reference of the object is of type Vehicle which is NOT implementing the interface?
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GOT IT
THANKS
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rikesh Desai wrote: Vehicle a = new Car(); //Line 11
Colorable i = (Colorable) a; //Line12

Line 11 is creating a reference of Vehicle and an object of Car.
Why Line 12 is not throwing a ClassCastException - as the reference of the object is of type Vehicle which is NOT implementing the interface?




I am still confused!
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which line you are confused Line 12 or 19 or somewhere else ?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rikesh Desai wrote:

Rikesh Desai wrote: Vehicle a = new Car(); //Line 11
Colorable i = (Colorable) a; //Line12

Line 11 is creating a reference of Vehicle and an object of Car.
Why Line 12 is not throwing a ClassCastException - as the reference of the object is of type Vehicle which is NOT implementing the interface?




I am still confused!



They are not talking about that.. That's perfectly legal and doesn't throw exception because a is an instance of Car polymorphed as a Vehicle so you can downcast it to a Colorable because Car implements Colorable.

However b is an instance of Vehicle which doesn't implement Colorable and so you can't cast it as such.
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Harikrishna - i was confused in line 11 and 12.
@See and Harikrishna - thanks guys.. i think i have understood it... it all depends on the object type and not on the reference type..
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Makes perfect sense. BMW extends Car and Car implements Colorable. So if BMW IS-A Car, Car reference can point to a BMW reference eve without a cast although a cast is redundant and okay.

But Vehicle doesn't implement Colorable, so trying to cast a Vehicle with Colorable, doesn't make sense. Obviously a class caste exception.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Type 1-
Vehicle v = new Car();
Car c = new Car();
c=(Car)v; or v=(Car)c;

Both work and both are car objects. In the event of overriding, both car methods are used.


Type2-
Vehicle v = new Vehicle();
Car c = new Car();
v=(Car)c;

Works and Both invoke the car methods in the case of overriding. A Car IS-A vehicle.



Type 3-
Vehicle v = new Vehicle();
Car c = new Car();
c=(Car)v;

Exception in thread "main" java.lang.ClassCastException: Vehicle cannot be cast to Car cuz a Vehicle IS-not-A Car.

Here you go Rikesh Desai. These are the three formats
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Rohit for the detailed explanation!
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem!
 
a wee bit from the empire
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic