• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why methods inside an interface can't have body?  RSS feed

 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I know that the methods declared inside the Interfaces are abstract by default. But the question is why?
Let me simply it even further. Then why can't we have a body inside an abstract method. What I am missing here?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Kr Agrawal wrote:Then why can't we have a body inside an abstract method. What I am missing here?

You are missing the definition of abstract method. An abstract method is a method without a body.
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface basically represents a role. So if a class implements an interface then
it basically declares that it can play the role represented by the implemented interface.
Now, it is totally in the hands of the implementing class that how it plans to play the role...interface
need not interfere in it, so interface provides no behaviour to its methods.

Manish
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was essentially a design decision by the Java founding fathers to have extra "syntactic sugar" around the classes having
essentially only constants and only declaring abstract methods: to call them "interfaces" and to introduce the extra keyword "implements" with connection to them.
 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for patiently replying.
I found this post also very helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!