• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Code description

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you read my post ???

Regards ...
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not correct that interface implementation is HAS-A, it's IS-A.

Regards ...
 
I want my playground back. Here, I'll give you this tiny ad for it:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic