wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tell me the output? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Tell me the output?" Watch "Tell me the output?" New topic
Author

Tell me the output?

Manoj Mani
Ranch Hand

Joined: Mar 31, 2007
Posts: 65
class MyClass
{
static int maxElements;

MyClass(int maxElements)
{
this.maxElements = maxElements;
}

}

public class Q19
{
public static void main(String[] args)
{

MyClass a = new MyClass(100);
MyClass b = new MyClass(100);

if(a.equals(b))
System.out.println("Objects have the same values");
else
System.out.println("Objects have different values");
}
}


equals() method was not overrided but still it compiles fine.Why?


Impossible Is Often Untried.Its Time For Us To Change....
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Manoj,


Every class inherits Object class so as your class "MyClass" does; You class inherits Object(daddy of all classes) class's equals() method whose definition is as following:


public boolean equals(Object o) {
return this==o;
}

So when you do like
if(a.equals(b)) {
}
a and b are objects of MyClass, so where Object is required as Argument any object of Java can fit there.


Regards,
cmbhatt


cmbhatt
Manoj Mani
Ranch Hand

Joined: Mar 31, 2007
Posts: 65
ok thanks.........
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Hello,

It compiles fine and gives the print in else part. The parent class for all classes in java is java.lang.Object class got default implementation for equals method.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tell me the output?