• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instanceof

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , Suma,Carl Trusiak

I really appreciate your effort and time. i got the point.
regards
vivek
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic