Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code description

 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you read my post ???

Regards ...
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Vassili Vladimir

Sorry,Sorry

i haven't seen the upper statement.

sorry please
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface implementation is HAS-A ?

Read, read and read more ...

Good luck ...
 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the output comes because of Has-A relation ship. Am I correct


Thank you

for the kind reply.
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not correct that interface implementation is HAS-A, it's IS-A.

Regards ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic