Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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.
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic