RunTime Polymorphism Related Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vishal Agrawal
    New Member
    • Feb 2008
    • 1

    RunTime Polymorphism Related Question

    When a super interface variable holds a subclass object, then according to run time polymorphisms we can access those methods which are declared in super interface or has been overridden in subclass. So my question is ..........

    How we get access to Object class methods like equal() on that reference variable, which is opposed to runtime polymorphisms, because equal() method has not been declared in Super Interface for which I'm taking about.

    Please do answer this question. I'm hanking after this question

    Vishal Agrawal
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    It never hurts to illustrate your question with sample code, especially when it's not totally clear. I think you are asking this:

    [CODE=Java]interface I {
    void m();
    }

    class Test {
    void f(I x, Object another) {
    if (x.equals(anoth er)) { //how can I do this?

    }
    }
    }[/CODE]
    The answer is go ahead and do it -- it's legal and works as you expect.

    Formally, in the JLS section 9.2: http://java.sun.com/docs/books/jls/t...faces.html#9.2

    If an interface has no direct superinterfaces , then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.

    Comment

    Working...