• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface and typecasting

 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface Colorable1 {}
class Vehicle1 {}
class bike1{}

public class Pool {

public static void main(String[] args) {
Vehicle1 b = new Vehicle1();
Colorable1 j = (Colorable1) b; -----> Compiles Fine
bike1 k = (bike1) b; -----> Compiler Error
}
}

In the above program Vehicle1 doesnot have any relation with Colorable1(Interface) and bike1(class), i.e it does not belong to same inheritance hierarchy

But i am able to typecast from Vehicle1 to Colorable1 even though there is no inheritance hierarchy ---> How
When i typecast from Vehicle1 to bike1 am getting compiler error, since they are not in inheritance hierarchy...

Why and how did that interface stuffs work?? Why not the class bike1 didnt work??
Can any body give suggestion on this please???
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please explain it briefly please...
 
Campbell Ritchie
Sheriff
Pie
Posts: 49842
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bike is a vehicle but a vehicle might not be a bike.
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But there is no inheritance in the example... Bike is not inherited from Vehicle in the example

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh Shanmugam wrote:
Why and how did that interface stuffs work?? Why not the class bike1 didnt work??
Can any body give suggestion on this please???


There may be a situation, that any sub classes of class implements that interface, so in java, type casting to an interface is allowed. But for class, if those classes were not in the same hierarchy, the Compiler definitely flag an Error. For interfaces, it can't sure this.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49842
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh Shanmugam wrote:
But there is no inheritance in the example... Bike is not inherited from Vehicle in the example

I didn't notice that, sorry. In which case it is obvious to the compiler that this cast will fail, so it throws an error.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49842
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote: . . . For interfaces, it can't sure this.
Have look at the Java™ Language Specification; there is bound to be something useful there.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Abimaran Kugathasan wrote: . . . For interfaces, it can't sure this.
Have look at the Java™ Language Specification; there is bound to be something useful there.


Could you Please spot the problem here? I couldn't get, what you are trying to tell!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49842
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look through the JLS as quoted and see whether you can find anything about casting or type conversions. You can easily find its index from the link I gave you.
 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i understand it...Vehicle1 and bike1 are classes and are not related, so you cant typecast one to other.
BUT you can typecast Vehicle1 to Colorable1 because Colorable1 is interface and in view of compiler, there can be class at runtime that inherits Vehicle1 and implements Colorable1. So in compiler's view, This is legal. ofcourse if this is not the case, it will cause run time error( as it will be in your program).

 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you create a class at run-time , and that too implementing that interface?? Is it really possible, and the compiler thinks, you might possibly do it?
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:How can you create a class at run-time , and that too implementing that interface?? Is it really possible, and the compiler thinks, you might possibly do it?


I think its not runtime. If you create a class xyz which extends Vehicle1 and implement Colorable1, then the type casting is valid.
Otherwise you will get a runtime exception.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if,


I very well accept these are possible!

How is this possible at all?? Isn't Vehicle1 and Colorable1 independent.
We are trying to give the Colorable's view of xyz to Vehicle1, but, Vehicle1 class has no idea about the methods in the Colorable interface as it does not implement it. Also, it will not have any idea that its subclass may implement this interface.
Can anyone explain this concept, please......
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:

How is this possible at all?? Isn't Vehicle1 and Colorable1 independent.
We are trying to give the Colorable's view of xyz to Vehicle1, but, Vehicle1 class has no idea about the methods in the Colorable interface as it does not implement it. Also, it will not have any idea that its subclass may implement this interface.
Can anyone explain this concept, please......


OK, Did you go through the link, which Campbell Ritchie gave?
If S is an interface type, and If T is a class type that is not final, then the cast is always correct at compile time (because even if T does not implement S, a subclass of T might)


So, make your Vehicle class as final class, and do the casting as you provided, then check the compilation. You'll get it. There may be possibility, that any of the sub classes of the Vehicle class implements this interface, so for that sub class' object, it can be referenced by Vehicle reference variable and Colorable reference variable, but actual object is from that sub class. So it can up caste to a super class(in your case, Vehicle).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49842
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote: . . . make your Vehicle class as final class, . . . sub classes of the Vehicle class . . .
Can you see the contradiction in what you wrote?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Abimaran Kugathasan wrote: . . . make your Vehicle class as final class, . . . sub classes of the Vehicle class . . .
Can you see the contradiction in what you wrote?


Those are not related to each other,
Abimaran Kugathasan wrote:
There may be possibility, that any of the sub classes of the Vehicle class implements this interface......


This is for the case, which he defined(Vehicle is not final class). If he defined Vehicle as final class, then he can't do this kind of casting. This is the think, I want to tell him.
 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:Even if,


I very well accept these are possible!

How is this possible at all?? Isn't Vehicle1 and Colorable1 independent.
We are trying to give the Colorable's view of xyz to Vehicle1, but, Vehicle1 class has no idea about the methods in the Colorable interface as it does not implement it. Also, it will not have any idea that its subclass may implement this interface.
Can anyone explain this concept, please......


" Vehicle1 vehicle=(Colorable1)new xyz();" is not right, This gives compiler error when compiled.
You are type casting new object of class xyz to Colorable1 type. You can assign it only to variable of type Colorable1.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry that must have been

I was about to ask, how compiler does not throw an error on this, if

exists though.
I still strongly feel, Colorable and Vehicle1 are independent.
Also,isn't it -A subclass depends on a superclass?
Somebody said, if a subclass of Vehicle1 implements Colorable, then Vehicle1 can be cast with Colorable. - This makes: 'A super class depends on its subclass'
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:sorry that must have been



Did you check it? It should give compilation error, those are not compatible types!
 
Jatin Dhingra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:sorry that must have been

I was about to ask, how compiler does not throw an error on this, if

exists though.
I still strongly feel, Colorable and Vehicle1 are independent.
Also,isn't it -A subclass depends on a superclass?
Somebody said, if a subclass of Vehicle1 implements Colorable, then Vehicle1 can be cast with Colorable. - This makes: 'A super class depends on its subclass'


Relationship between superclass and subclass is "IS A" not depends on. See corey's article http://radio.javaranch.com/corey/2004/04/05/1081179854000.html
this might be helpful.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic