• 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

doubt in cast

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface Bicycle{
public void Bicycle();
}
public class Jchq {
public static void main(String argv[]){
Jchq j = new Jchq();
Bicycle b = (Bicycle) j;
}
}

what should be the answer the compile time error or runtime error
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gala

This is not compile time Exception

It showed the runtime Exception because , we did not implement the interface.

There is no possible to convert class object to interface object.

Which showed the java.lang.ClassCastException.

Check and tell me your feedback.
 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes It is giving runtime exception
But I have a doubt that why compiler doesnot give error
Since Jchq and Bicycle are not in the same inheritance tree it should give error
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes V Gala same doubt here !
 
krishnamoorthy kitcha
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys

This is the good question, In the compile time everything take it as a java object , not class or interface or others.

It should be identify only in the run time, because ,the compile time only

which is the object belonging to that.

That's why showed the runtime Exception.

Are you clear or not ?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kitcha are you learning English ??
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
V Gala:

The problem in your code is that you are trying to instantiate Jchq within its own definition. I don't think that you can do that. I had a similar problem before.

If you comment out the instantiation and casting statements it does compile:

interface Bicycle{
public void Bicycle();
}

public class Jchq {
public static void main(String argv[]){
//Jchq j = new Jchq();
//Bicycle b = (Bicycle) j;
}
}

Originally posted by V Gala:
interface Bicycle{
public void Bicycle();
}
public class Jchq {
public static void main(String argv[]){
Jchq j = new Jchq();
Bicycle b = (Bicycle) j;
}
}

what should be the answer the compile time error or runtime error

 
rey leon
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by the way, the anser is ompile time error.

Originally posted by V Gala:
interface Bicycle{
public void Bicycle();
}
public class Jchq {
public static void main(String argv[]){
Jchq j = new Jchq();
Bicycle b = (Bicycle) j;
}
}

what should be the answer the compile time error or runtime error

 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its always possible to cast to interface type unless the object is of final class.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

Ahmed Yehia posted

Its always possible to cast to interface type unless the object is of final class.



The reason why this is allowed is that you could extend a non-final class and additionaly implement this interface.
To allow a polymorphic use of these kind of classes, the cast must be legal.

Example: When you additionally to Jchq and Bicyle compile this:

It also compiles and there is no exception. It prints:
MountainBike Bicycling



By the way, not related: Eclipse 3.1 IDE warns us about class Bicycle: "This method has a constructor's name".


Yours,
Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic