• 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: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above code is actually given in kathy book pg:526.when compiled it gives an err sayin inconveritble types in line 1. They have explained this prg in the book but i do not understand this.. please explain
[ August 10, 2008: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hari,
Your code compiles and runs without modification.
Except after beautifying the code.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is perfect, and its running. Try again.

Explanation :

1) In class EqualsTest you are creating two instances of Moof Class, and then call the equals method in If(one.equals(two)) inside the EqualsTest class. (Simple...)

2) In Moof class your are using argument construtor to assing value to instance variables. and also created getMoofValue() method to get value of instance variable (Again Simple...)


3) The second method in Moof Class is equals() method, which you are calling from EqualsTest class.

Inside if() you are calling equals() method on object named 'one' and passing the object named 'two'.

Now inside equals() method, you are checking if object passed two is instance of class Moof using instanceof operator.

Now big question how instanceof operator works.

(o instanceof Moof) -> instanceof operator check for is-A relationship between a object and class.

is-A relationship means like this

class A { }
class B extends A { }
class C { }

There is three classes. and we create object of all of them and then apply instanceof opertor to them.

A a = new A();
B b = new B();
C c = new C();

(a instanceof A) return true, because every object is instance of its class
(b instanceof B) return true, same logic
(b instanceof A) return true, because class B is extended from class A
(c instanceof C) return true,
(c instanceof A) return compiler error, because class C is not extended from class A
(c instanceof B) return compiler error, because class C is not extended from class B

System.out.print(a instanceof A); // prints true;


Back to if() part of equals() method.

Now after checking whether 'o' is an instanceof Moof, we are casting 'o' to Moof type so that we can use getMoofValue() method on that.

((Moof)o).getMoofValue(); this statement is equivalent to these two lines


1. Moof ob = (Moof)o;
2. ob.getMoofValue();

Note : you should read kathy sierra's book page no 283-285, 524-529, 90 (again read them)
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks vijaygupta, you have nicely explained the prg. but the prob is i still did not get the output for the program unless i have changed the line,
public boolean equals(Object o)
as,
public boolean equals(Moof o)
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the equals method parameters must have only 'Object' i suppose, then where is the prob, is there anything that i have understood wrong or..
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone reply me... please... :roll:
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by hari harann:
Can someone reply me... please... :roll:

Please read this.
 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am really sorry.. by the way am working out with my problem to find the solution..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic