Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

INTERFACE

 
Utkarsh Chowdhary
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Thanks

[This message has been edited by Rahul Mahindrakar (edited March 13, 2001).]
 
Mark Savory
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1868
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

------------------
Regds.
Mahindrakar
 
Mahendra Kamble
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
t(i);
}
here u get compiler error *****Hero
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic