1

I was practicing to program for interface in java when I came across something I could not understand. Let me call the class Country

 class Country implements Comparable
    {
    int a; 
    int b; 
    int c;
    public Country(int _a,int _b,int _c)
    {
    a=_a;
    b=_b;
    c=_c;
    }
    public int compareTo(Object obj)
    {
    /*compares a, b and c and returns number of variables greater for this object.I am not include instanceof check*/
    int count=0;
    Country other=(Country)other;
    if(a>other.a)
    count++;
    else 
    if(a<other.a)
    count--;
    if(b>other.b)
    count++;
    else 
    if(b<other.b)
    count--;
    if(c>other.c)
    count++;
    else 
    if(c<other.c)
    count--;
    return count;
    }
public void write()
{
System.out.println(" hello");
}
    public static void main(String args[])
    {
    Object p=new Country(1,2,3);
    Object q=new Country(2,3,4);
    System.out.println(p.compareTo(q));
    }
    }

So the question here is if we declare something as

Object p=new Country(1,2,3);
Object q=new Country(2,3,4); 
p.write(); 

This works. but why not

p.compareTo(q)//as done in the main code

Why is this cast required?

((Comparable)p).compareTo(q);
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Note: You should be using `class Country implements Comparable`—avoid [raw types](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). – Slaw Jun 24 '19 at 08:11
  • `p.write(); ` does **not** work neither, at least for me. Not sure what you've done for it to work. – Amongalen Jun 24 '19 at 08:14
  • Also, I strongly doubt that your implementation of `compareTo` meets the [formal requirements](https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html) of the `Comparable` interface – Andy Turner Jun 24 '19 at 08:14
  • @Slaw I do know about the generic interface but my question is about this one in particular. – dawood mansoor Jun 24 '19 at 08:19
  • @Andy Turner yes it does not because it does not follow x.compareTo(y)=-y.compareTo(x); – dawood mansoor Jun 24 '19 at 08:20
  • @Andy Turner yes i do not feel it is a duplicate . please request the removal of duplicacy. – dawood mansoor Jun 24 '19 at 08:21
  • 1
    @GhostCat yes the question of which has been a duplicate answers my question. Oh it is just so difficult to ask a non duplicate question. – dawood mansoor Jun 24 '19 at 08:32
  • @dawoodmansoor "Oh it is just so difficult to ask a non duplicate question" that means that you're not alone in things you find confusing :) It's sometimes hard to know what to search for, though, and those of us who have been around here for a long time can find it easily. – Andy Turner Jun 24 '19 at 08:36
  • @AndyTurner I realized that when I saw the headings of the above questions. I will get used to it , god willing. – dawood mansoor Jun 24 '19 at 08:40
  • @lealceldeiro yes they answer it too. i have read them. – dawood mansoor Jun 24 '19 at 09:22

2 Answers2

2

Because you are storing the value in a reference of type Object, so, the compiler has no way to know that it is actually a Country and that it implements Comparable at all.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
2

Here:

Object p=new Country(1,2,3);

The compiler could know that p is of type Country. But it doesn't know it.

It only sees that you declared p to be of type Object. And the class Object does not have a print() method, or a compareTo() method.

Polymorphism (looking up methods on the actual type of objects) happens at runtime, but deciding whether some object has a specific method happens at compile time. And in this case, the compiler decides: p is an Object, thus it is missing the method you want to invoke!

GhostCat
  • 137,827
  • 25
  • 176
  • 248