jQuery in Action, 3rd edition
The moose likes Threads and Synchronization and the fly likes INTERFACE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "INTERFACE" Watch "INTERFACE" New topic


Utkarsh Chowdhary

Joined: Feb 27, 2001
Posts: 6
Interfaces are designed to support dynamic method resolution at run time.
Please explain this.

Does static void t(CanFight x)
means CanFight i=new Hero();
if yes then how it can refer to a ActionCharacter fight() method?

Please use [ code] [ /code] tags to format your code. Check out the UBB Code link below for further details


[This message has been edited by Rahul Mahindrakar (edited March 13, 2001).]
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
Let me take a stab at this. Hero implements the CanFight interface which means that you can declare a variable of type CanFight that references a Hero object. Hero has one implementation of 'fight()' which it inherits from ActionCharacter, so whether you say "CanFight i = new Hero(); i.fight();" or "Hero i = new Hero(); i.fight();" doesn't matter.
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324

if yes then how it can refer to a ActionCharacter fight() method?

Utkarsh- Note this interesting fact - An inherited method CAN implement an interface on behalf of the subclass !!
ActionCharacter's fight() is public and hence is inherited by Hero class. And this inherited fight() method implements the CanFight interface on behalf of Hero.
And this is despite the fact that the ActionCharacter class where fight() is originally defined does not 'implement' CanFight interface !! And despite the fact that Hero itself is defined to 'implement' the interface but does not itself define the method of the interface !!
It is because the inherited fight() method is implementing the CanFight interface on behalf of Hero, that Hero class is not abstract, although the Hero class itself does not define the method fight() of CanFight interface.
Thus a variable of type CanFight can hold a reference to an object of type Hero (An interface type variable can hold reference to object of a class implementing that interface type).

[This message has been edited by Rahul Rathore (edited March 13, 2001).]
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1868
You already refer to ActionCharacter's Fight method!!!.
Check out the code below

The above code prints out
in Action Character
in Action Character
This is a System.out.println statement in the ActionCharacter's fight method

Mahendra Kamble

Joined: Mar 13, 2001
Posts: 4
hello utkarsh,
Yor code will definately run
your fight() method is define in actioncharacter class which is invoked using refrence of interface which is always possible in conversion of object using that interface.

public static void main(String a [])
***** hero i=new hero();
here u get compiler error *****Hero

Mahendra Kamble<BR>Software Engineer
I agree. Here's the link: http://aspose.com/file-tools
subject: INTERFACE
It's not a secret anymore!