473,465 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

why is the equal method of Object class is getting called?

3 New Member
Expand|Select|Wrap|Line Numbers
  1. final class Item {
  2. Integer size;
  3. Item(Integer size) { this.size = size; }
  4. public boolean equals(Item item2) {
  5. if (this == item2) return true;
  6. return this.size.equals(item2.size);
  7. }
  8. }
  9. public class SkepticRide {
  10. public static void main(String[] args) {
  11. Item itemA = new Item(10);
  12. Item itemB = new Item(10);
  13. Object itemC = itemA;
  14. System.out.println("|" + itemA.equals(itemB) +
  15. "|" + itemC.equals(itemB) + "|");
  16. }
  17. }
Jun 1 '10 #1
4 1904
jkmyoung
2,057 Recognized Expert Top Contributor
itemC is declared to be an Object, not an Item. If you want it to call the Item's equal method, you need to explicitly cast it as an Item:
((Item)(itemC)).equals(itemB);
Jun 1 '10 #2
ankitc7
3 New Member
But doesn't the run time polymorphism apply here ??

A super class reference variable(Object's) item c is pointing to a sub class object(Item A)..so the overridden method should be called..like in tis code:

Expand|Select|Wrap|Line Numbers
  1. class superclass {
  2. public void func() {
  3. System.out.println("superclass");
  4. }
  5. }
  6.  
  7. class subclass extends superclass{
  8. public void func() {
  9. System.out.println("subclass");
  10. }
  11. }
  12.  
  13. public class SkepticRide {
  14. public static void main(String[] args) {
  15. superclass sup=new subclass();
  16. sup.func();
  17. }
  18. }
  19.  
  20. here subclass func wud be called
Jun 1 '10 #3
jkmyoung
2,057 Recognized Expert Top Contributor
I'm not 100% sure but:
It has to do with the fact that your function uses Item as opposed to Object.
public boolean equals(Item item2)

Object does not have a public boolean equals(Item item2)
method, so it does not know to call Item's .equals(Item) function.

If you rewrote your function as
public boolean equals (Object item2)
then it would probably call the correct function.
Jun 1 '10 #4
ankitc7
3 New Member
yes you are correct...this has nothing to do with polymorphism...thanks a lot!!
Jun 1 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: brulsmurf | last post by:
Ok iam despered, in Main of this code the object 'two' is looking as should be when i print it, but the exact same code in procedure "duTest" makes the object filled up with additional strange...
1
by: shel | last post by:
Hi All, Probably a silly question to those who know, but i'm a noob with C# :D I have a COM component written in VB6 I'm trying to interop to from C# (using the IDE 'Add Reference' capability to...
1
by: oj | last post by:
Could someone explain to me why the following code snippet throws an "Object Reference not set to an instance of an object error": public void SetActiveStaffMember(int staffMemberId) { ...
5
by: Ram | last post by:
Hi Friends I want to develope a custom control in .net which can be used with any project. I am writing a function in that class which I want to take any object as parameter. For that I have...
7
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be...
2
by: Aryan | last post by:
Hi, I am implementing Business object, but while implementing this I want to have DataTable or DataSet as part of Business Object. For example, I want to bind controls directly to these Business...
1
by: girays | last post by:
I have a template class which name is EntityRepository and when I compile this class I get no error. But when I use this class in a main method I get LNK2019 linking error. std::map object is used...
3
by: mak2045 | last post by:
I copied a form (Microsoft 2000 with Visual Basic 6.0) that had a procedure behind the text box and renamed it. I did not get an error message, but the new form that I thought I created did not...
5
by: mikie | last post by:
somebody asked me a question: string is derived from object class, right? (i said yes) so how it could be object class declares method returning string? thanks for any explanation / link /...
4
by: pjerald | last post by:
Clear me friends. Object class is the base class for all classes. Thus the methods with protected access in the object class can be invoked by any object(Of any class). I know the statement is...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.