This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof?

 
hari harann
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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 ]
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hari,
Your code compiles and runs without modification.
Except after beautifying the code.
 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Can someone reply me... please... :roll:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Am really sorry.. by the way am working out with my problem to find the solution..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic