• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces (Output 's Confusion)  RSS feed

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers ,

Why does this code always call fight() method of the interface
though class ActionCharacter has its own fight() method

Even though method i am calling fight() of ActionCharacter with

[ static void w(ActionCharacter x) { x.fight(); } ]


interface CanFight {
void fight();
}

interface CanSwim {
void swim();
}

interface CanFly {
void fly();
}

class ActionCharacter {
public void fight()
{
System.out.println("Inside ActionCharacter");
}
}


class Demo extends ActionCharacter implements CanFight, CanSwim, CanFly
{
public void swim()
{
System.out.println("Inside swim");
}
public void fly()
{
System.out.println("Inside fly");
}


public void fight()
{
System.out.println("Inside fight");
}
}


class Adventure {

static void t(CanFight x) { x.fight(); }

static void u(CanSwim x) { x.swim(); }

static void v(CanFly x) { x.fly(); }

static void w(ActionCharacter x) { x.fight(); }


public static void main(String[] args) {
Demo h = new Demo();
t(h);
u(h);
v(h);
w(h); // Treat it as an ActionCharacter
}
}

--------------------------------------------------------------------------

Beside why the class Demo does not become abstract
if i dont override fight() of the Canfight interface
after implementing Canfight


like

class Demo extends ActionCharacter implements CanFight, CanSwim, CanFly
{
public void swim()
{
}
public void fly()
{
}

}


Even now i am able to instantiate an object of Demo , though here i am not
overriding Canfight interface ?
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In method w(h) you are passing demo and that method takes w(ActionCharacter), so image code something like this

Demo d = new Demo();
ActionCharacter x = d; //where demo extends actioncharacter

Now x refers to demo object on the heap but it is of type actioncharacter, so you are right compiler looks that ActionCharacter has fight() method and everything is fine and so it compiles. At run time JVM sees that fight() method has been overridden in demo class and thus calls the fight() method of that demo class. This is an example of Polymorphism.

Now even if you remove fight() method from Demo class and extend canFight interface in the class it will compile fine, because demo class extends ActionCharacter which already has fight() method and demo class has inherited it,so demo class too has fight() method. makes sense?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand your question. A method is never implemented in an interface. If the method is executed, it's because you implemented it in the class definition.
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok jay ,

but if you see for the first code i am getting output as

Inside fight
Inside swim
Inside fly
Inside fight

rather than

Inside fight
Inside swim
Inside fly
Inside ActionCharacter


why ?
[ March 08, 2006: Message edited by: faisal usmani ]
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because demo class extends ActionCharacter and overrides it's fight method. So from code you posted ActionCharacter's fight method will never be called unless you do this:

ActionCharacter a = new ActionCharacter();
a.fight();

Does it makes sense? You can read a bit about polymorphism and overriding and I am sure things will be more clear.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another pass at it...

The CanFight interface has a fight() method, and the ActionCharacter class has a fight() method. (So far, these methods are really unrelated, because ActionCharacter does not implement CanFight.)

The Demo class extends ActionCharacter and implements CanFight. This means that Demo inherits ActionCharacter's implementation of the fight() method, so there is no need to provide any additional implemention of fight() within Demo. The inheritance alone satisfies the interface contract.

Although it's not required to, Demo also overrides ActionCharacter's implementation of fight().

So now whenever we have a runtime instance of Demo, the Demo implementation of fight() will be invoked via polymorphism. It doesn't matter whether the Demo reference is upcast to type ActionCharacter or CanFight. The true runtime type (Demo) determines which method implementation is invoked.

Here is an illustration that breaks it down...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!