wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Code description Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Code description" Watch "Code description" New topic
Author

Code description

Gaurav Pavan Kumar Jain
Ranch Hand

Joined: Mar 19, 2007
Posts: 168
public class test1{
public static void main(String args[])
{
I i1=new A();
I i2=new b();
A a =new A();

System.out.println(i1 is Instanceof I);// output True
System.out.println(i2 is Instanceof I);// output True
System.out.println(a is Instanceof I);// output True

}
}

interface I{}
class A implements I{}
class B implements I{}


please explain ?
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

When your class implements an interface, your class is said to be of that type, in other words, IS-A that type.

The first line outputs true, because i1 is an I.
The second line outputs true, because i2 is an I as well.
The third line outputs true, because a is an I.

Please note the interface and classes hierarchy.

Please also note that you can't say


It will give a compilation error, you must use this

Instead.

Best of luck ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi
instance of keyword is used to check "is-a" relation

i1 is refering to A
but A implemetns I

So the is-a relation ship exists
that's you are getting true
the same reason for the other statements also
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Vassili Vladimir


It will give a compilation error, you must use this

code:
--------------------------------------------------------------------------------

System.out.println(i1 instanceof I);

----------------------------------------------------------------------------
Please check this statement once

I think i will not give compile time error

Why because
at compile time the compiler checks wheather there is an is-a relation ship
between i1 and I.Here the relation is there ofcouse here the relation is not is-a but i1 is a reference Of I so it compiles.

And at run time i1 is refering to A .Now again it will checks. so now also relation succeds.
So there is no problem.

please check this once
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
did you read my post ???

Regards ...
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

Vassili Vladimir

Sorry,Sorry

i haven't seen the upper statement.

sorry please
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Don't sorry me my friend, we all here to correct the mistakes of each other and help each other

Best Regards ...
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Thank you
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
When your class implements an interface, your class is said to be of that type, in other words, IS-A that type


It is not correct. The relationship is HAS - A when a class implements an interface.
When a class extends another class, the relationship is IS - A

bye for now
sat
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi,


instanceof checks for Assignment Compatibility.

public boolean equals(Object o){

Prototype:
if(o instanceof [Interface,AbstactClass,ConcreteClass])
{
[
Interface i =(Interface) o;,
AbstractClass ac = (AbstractClass) o;,
ConcreteClass cc = (ConcreteClass) o;
]

}

Have a nice day.

[ March 29, 2007: Message edited by: Srinivasan thoyyeti ]

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
interface implementation is HAS-A ?

Read, read and read more ...

Good luck ...
Gaurav Pavan Kumar Jain
Ranch Hand

Joined: Mar 19, 2007
Posts: 168
So the output comes because of Has-A relation ship. Am I correct


Thank you

for the kind reply.
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
It's not correct that interface implementation is HAS-A, it's IS-A.

Regards ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Code description
 
Similar Threads
Question on "instanceof"
Strange Behaviour Of Wrapper Class
keyword "new"
Strange Behaviour Of Wrapper Class