Question

I cannot figure out the difference between accessing a method of a subclass that is accessed and overwritten using the base class(Object) for the static type of the object versus using the subclass(Point).
For example:

public class Point {
int x, y;
...
public boolean equals(Object o){
...
}

public boolean equals(Point p){
...
}
}


Object o = new Object();
Point p = new Point(3,4);
Object op = new Point(3,4);

// here the static type is Point and the dynamic type is point, in this case 
// the equals() method that we choose and gets overwrriten depends on the
// parameter type that is passed. 
p.equals(o);
p.equals(p);
p.equals(op);

// here however the static type is Object so we initially look at the equals() 
// method in our Object class during compile time and check its parameter type   
// which is Object, thus if we overwrite      
// the equals() method we will pick the one that has a type Object parameter. 
// Since the dynamic type of our object is Point, when we are at run time
// we will look for the  equals() method in Point that matches with the   
// method type Object parameter.

op.equals(o);
op.equals(op);
op.equals(p);

What i am not seeing is why would i want to use the later rather than the former to specify what method i would like to overwrite? The former depends on the type parameter while the later depends on the type parameter of the static type of our object. I just dont see the benefit of using Basetype obj = new Subclasstype() to access and overwritten method in my subclass. It looks more complicated and the object can only be used to access instances of methods in the subclass that were in the base class, not any other methods in the subclass.

Was it helpful?

Solution

public boolean equals(Point p){
  ...
}

The above method has nothing to do with equals(Object) except sharing the name equals. It does not override it, it cannot be invoked from an Object-typed variable, there is no contract for it defined by Object.

When you write

op.equals(p);

you are invoking a method on the Object type. The only method called equals in that type is equals(Object) and this choice is permanent. The runtime can only provide a different override of equals(Object), but it will never route the call to equals(Point).

Similarly, when you write

p.equals(op);

the compiler will again see that you are invoking equals with an argument whose static type is Object, selecting equals(Object).

OTHER TIPS

I think you are messing up some concepts, so let me explain them:

Object op = new Point(3,4);

The behavior of variables o and p are clear, so I'll focus on op: it's a variable whose type is Object, which means you can use all methods declared in that class. You cannot use equals(Point p) method because it does not exist for Object. But, at the same time, the behavior of a method is given by the instance it self, not by the variable which is pointing at it, so op is pointing to a Point instance and the equals(Object) method has been overwritten, so the behavior of that method is given by the class Point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top