• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

INTERFACE

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic