• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface in java

 
syruss kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all,

I have created 2 interfaces with same method signature and implemented that in a class like below

Interface a{

public void test();
}

Interface b{

public void test();
}


class PlayJava implements a,b{
@override
public void test(){

}


1.why its implemented only one method its even throwing any compilation error or runtime error?
2.How JVM handle this.

thanks in advance



}
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. This will not result in compilation error as JVM considers each a valid Java entity.

2. Java will execute the method implemented in the PlayJava class.
 
syruss kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks devaki,

Yes it will not throw any compilation exception.It will work perfectly but how JVM handles these kind of scenarios that im really confusing .

Please can you clarify me.

Thanks in advance
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What compiler sees that if you are implementing the interfaces then you have to override all the methods defined in it.
so your class follows that contract.
There in no ambiguity for the compiler in calling the method.

But if you take the case defined below



at line 1 there is an error from compiler which says "The field value is ambiguous"
so you you want to access a particular constant you have to access it line <interface>.<variable>

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic