About Question enthuware.ocajp.i.v7.2.837 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.837 :
Hi,
Can we actually say, as a result of this question, that the operator "instanceof" works at compilation time for classes and at runtime for interfaces?
Thanks in advance.
The_Nick
Can we actually say, as a result of this question, that the operator "instanceof" works at compilation time for classes and at runtime for interfaces?
Thanks in advance.
The_Nick
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.837 :
Partially true. The compilation fails in some cases only because the compiler is able to figure out that the reference can never point to an object of the class on the right hand side. But the real type checking happens at run time.
With interfaces, it is always possible (because any class can implement multiple interfaces) so it doesn't do any type checking at compile time.
With interfaces, it is always possible (because any class can implement multiple interfaces) so it doesn't do any type checking at compile time.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.837 :
admin wrote:Partially true. The compilation fails in some cases only because the compiler is able to figure out that the reference can never point to an object of the class on the right hand side. But the real type checking happens at run time.
With interfaces, it is always possible (because any class can implement multiple interfaces) so it doesn't do any type checking at compile time.
OK So basically we can say that for interfaces the checking of whether is an instance or not it's made at runtime. The compiler cannot see that the other class implements a certain interface? On the other hand it does so with classes, the compiler is able to see whether or not a class is "of the same family" so why sh
uld not it see that it implements an interface?
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.837 :
Because it is possible that a new class that implements that interface may become available and the reference may point to an instance (which can be created using reflection) of that class.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Thu Jan 15, 2015 1:46 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.837 :
With very similar code I get an error message "inconvertible types". Specifically, when I apply instanceof to an instance of a class that has no ancestors other than Object and evaluate it to see if it is an instance of any other class type I get this error. I have compiled and run your code on my machine and it works as advertised. I don't understand why (b instanceof Flyer) doesn't produce the "inconvertible types" error. Any help you made provide is greatly appreciated. Thanks.
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.837 :
Please read my post just above yours. It explains why a compiler may not be able to generate an error with an interface.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Jul 16, 2017 4:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.837 :
In order to understand this topic better, I have written the following code example in order to prove it's a good thing Java does not check for "incompatible types" in case of an instanceof of an interface:
Result:
Code: Select all
interface Flyer{ }
class Bird implements Flyer { }
class Eagle extends Bird { }
class Bat { }
class SubBat extends Bat implements Flyer {}
class TestClass {
public static void main(String[] args) {
Flyer f = new Eagle();
Eagle e = new Eagle();
Bat b = new SubBat();
if(f instanceof Bird) System.out.println("f is a Bird");
if(e instanceof Flyer) System.out.println("e is a Flyer");
if(b instanceof Flyer) System.out.println("b is a Flyer");
}
}
Code: Select all
f is a Bird
e is a Flyer
b is a Flyer
Who is online
Users browsing this forum: pavvel and 40 guests