aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof" Watch "instanceof" New topic
Author

instanceof

Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi,
I need help of java experts. this is regarding instanceof operator.
(LH instanceof RH) return true of false
I know that LH can be a variable of type object refrence or may be a array element. RH side must me a class, interface or array.
i understand the class and array part.
can some one please put some light on interface as RH. i will really appreciate if some one explain it with example.
regards
vivek
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
Hi vivek,
In RHE it is mentioned that:
The right-hand operand may equally well be an interface. In such a case, the test determines if the object at the left-hand argument implements the specified interface
Consider this.
There is a class A and it implements B and if a is the object of A,(i.e. A a = new A()), then
a instance of B will return true.

Also, if there is a class C which is a subclass of A, and c is an object of C then
c instance of B. will return true.
I have written a small program to explain this.

When you run this program, you will get the result as:
true
true
false.
I hope the above explanation helps you
Suma

[This message has been edited by Suma Narayan (edited June 14, 2000).]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
This is to determine if this object is a class (or subclass) that implements an Interface.
example:
public class Test implements Runnable
{
....
}
public class Test2 extends Test{
...
}
public static void main(String[] args)
{
Test2 t = new Test2();
if(t instanceof Runnable)
t.start();
}

This is particularly useful if you have a subclass and need to ensure that the super class implemented an interface.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
I am relatively new to java. How do u compile the above program
(i know javac file name), but the above code has got three classes like NoInstance, Instance and subInstance and interface.what should be the name of the program NoInstance.java
or Instance.java or subInstance.java
i tried with subInstance.java it is working., can u please clarify because all the three classes are different(i mean not
inner classes)
can u please explain this


Bhasker Reddy
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi , Suma,Carl Trusiak

I really appreciate your effort and time. i got the point.
regards
vivek
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi Bhasker,
Your java source file can have as many classes as reuired but there will be only public class. so name of the source file should be same as of the public class.
vivek
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: instanceof