When I try to point the object of Car class with Colorable interface without using implements in class car i does not show any compilation error(although it shows runtime error) (Problem 1)and when i try to do the same with class that i do not extend vehicle class and try to point the object of car with vehicle class it immediately shows compilation error.why is it so?
Problem 1:
interface Colorable {}
class Vehicle {}
class Car extends Vehicle {}
public class Tester {
public static void main(String[] args) {
Car c=new Car();
Vehicle a = (Vehicle)c;
Colorable i = (Colorable)c;
System.out.println("Successful");
}
}
Problem 2:
interface Colorable {}
class Vehicle {}
class Car {}
public class Tester {
public static void main(String[] args) {
Car c=new Car();
Vehicle a = (Vehicle)c;
Colorable i = (Colorable)c;
System.out.println("Successful");
}
}
-
4When you were typing your question, there was a handy How to Format box right next to the editing area. It's worth reading. There was also a preview underneath where you could see what your question would look like, and a formatting toolbar above the editing area. As this isn't your first question, please do take the time to format things clearly before posting rather than relying on people like Rahul to do it for you.T.J. Crowder– T.J. Crowder2013年08月19日 12:28:59 +00:00Commented Aug 19, 2013 at 12:28
-
Problem 2: You cannot cast c to Colorable ("Colorable i = (Colorable)c;") since Car does not implement that interface. But it would be very helpful, if you could show us the error outputs you refer to.tbsalling– tbsalling2013年08月19日 12:30:54 +00:00Commented Aug 19, 2013 at 12:30
-
Probably you get at least a compile warning about unchecked casts...Stefan Neubert– Stefan Neubert2013年08月19日 12:35:38 +00:00Commented Aug 19, 2013 at 12:35
3 Answers 3
It's possible for an instance of a subclass of Car to implement Colorable, which is why it's not a compile-time error.
However, in problem 2 given that Vehicle and Car are entirely separate class hierarchies, the compiler knows it's impossible for any Car reference to also be a reference to an instance of Vehicle... so it can reject it at compile time.
Basically the difference between a class and an interface here is that an interface can be implemented further down the type hierarchy, whereas there's a single inheritance chain for the class hierarchy, which means the compiler can detect more problems.
If Car were marked as final, the compiler would likewise be able to tell that it was impossible for an instance of Car to also implement Colorable, and that would be a compile-time error too.
4 Comments
Vehicle isn't a subclass of Car - Car is a subclass of Vehicle.)Class hierarchy is known during compile time. Java immediately knows whether Car is-a Vehicle.
Interfaces are more tricky. Consider this code:
interface Colorable {}
class Vehicle {}
class Car extends Vehicle implements Colorable {}
{
// case 1:
Vehicle vehicle1 = new Vehicle();
Colorable colorable = vehicle1; // error
// case 2:
Vehicle vehicle2 = new Car();
Colorable colorable2 = vehicle2; // OK! Car implements Colorable!
}
Even though a Vehicle does not implement Colorable, a subclass of Vehicle might. Hence the runtime not compile time error.
2 Comments
In 1st case
Car c=new Car();
Vehicle a = (Vehicle)c;
Colorable i = (Colorable)c;
There are two thing -
- you are creating a Car object. Then you are using Vehicle reference to point to a car object which is valid since your Car class extends Vehicle class.
Then you are using Colorable reference to point to car object. Now it is possible that some sub class of car implement Colorable interface. For example
class myCar extends Vehicle implements Colorable{}
and then you do
Car c=new myCar();
Colorable i = (Colorable)c;
This should not throw runtime exception. In your case you are telling the compiler to trust you that there would be some sub class of class car that would implement Colorable interface but it is not. So you will nit get a compile time error but as you break that trust you will get a runtime error.