aspose file tools*
The moose likes Beginning Java and the fly likes instanceof? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instanceof?" Watch "instanceof?" New topic
Author

instanceof?

hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54

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

Joined: Aug 05, 2008
Posts: 347
Hari,
Your code compiles and runs without modification.
Except after beautifying the code.
V K Gupta
Ranch Hand

Joined: Aug 07, 2008
Posts: 55
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

Joined: Aug 07, 2008
Posts: 54
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

Joined: Aug 07, 2008
Posts: 54
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

Joined: Aug 07, 2008
Posts: 54
Can someone reply me... please... :roll:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61232
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
hari harann
Ranch Hand

Joined: Aug 07, 2008
Posts: 54
Am really sorry.. by the way am working out with my problem to find the solution..
 
Don't get me started about those stupid light bulbs.
 
subject: instanceof?