1

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");
 }
}
asked Aug 19, 2013 at 12:27
3
  • 4
    When 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. Commented 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. Commented Aug 19, 2013 at 12:30
  • Probably you get at least a compile warning about unchecked casts... Commented Aug 19, 2013 at 12:35

3 Answers 3

8

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.

answered Aug 19, 2013 at 12:34
Sign up to request clarification or add additional context in comments.

4 Comments

But if you take a following example class Colorable {} class Vehicle {} class Car extends Vehicle {} public class Tester { public static void main(String[] args) { Car c=new Car(); Vehicle a =c; Colorable i=c; System.out.println("Successful"); } }
@user2662247: then what? You haven't said what you expected to happen.
in above code it is also possible for a subclass of car(i.e Vehicle)to inherit the class colorable so if i write vehicle extends colorable then the program is succesfully executed but if i don't extend it in vehicle as i have shown in above example then it is a compilation error which is a run time error if i make the colorable interface instead of class.
@user2662247: Yes, for all the reasons stated in my answer. Which exact bit of that is confusing you? (Note that Vehicle isn't a subclass of Car - Car is a subclass of Vehicle.)
2

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.

RiaD
47.8k12 gold badges85 silver badges128 bronze badges
answered Aug 19, 2013 at 12:36

2 Comments

your above code is ok because vehicle2 reference variable holds the reference id of car which i am passing on to colorable2 which also can hold the reference id of car variable.But in case1 you are trying to assign vehicle class reference id to colorable reference variable which is not possible..for eg. consider code given in my next comment.
class Colorable {} class Vehicle extends Colorable{} class Car extends Vehicle {} public class Tester { public static void main(String[] args) { Car c=new Car(); Vehicle a =c; Colorable i=c; System.out.println(c); System.out.println(a); System.out.println(i); } }
0

In 1st case

Car c=new Car();
Vehicle a = (Vehicle)c;
Colorable i = (Colorable)c;

There are two thing -

  1. 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.
  2. 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.

answered Aug 19, 2013 at 12:49

1 Comment

Suppose i make the interface colorable to class colorable and if i extend that colorable class to vehicle class then i can point the car object with colorable reference variable.But if you now do not extend colorable to vehicle then againg it is a compilation error..but in case of interface it is runtime.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.