Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding interfaces

 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we declare a interface and declare methods in it -
Than this interface is implemented by a class
I this class both the methods of the interfaces are defined
But why do they have to be specified as public to work ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because interface methods are intrinsically public, and the point of an interface is to define an external contract.
 
Eoin Brosnan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik D mehta wrote: we declare a interface and declare methods in it -
Than this interface is implemented by a class
I this class both the methods of the interfaces are defined
But why do they have to be specified as public to work ?


Why are the methods in class that implements the interface forced to be public? That's pretty easy to explain.



By making the method private it's as if it doesn't exist- i.e it's like the interface wasn't implemented.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the situation I wanted to know about




[/code]
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we have to write public for boolean method


You have already got the answer from David Newton .

 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why do we have to write public for boolean method


You have already got the answer from David Newton

Because interface methods are intrinsically public. Classes are not.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Because interface methods are intrinsically public, and the point of an interface is to define an external contract.


I actually didn't get this can you please explain in a more simpler way .
 
Manjusha Muraleedas
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to have a basic understanding of java interface.

The following tutorial may help you...Interface Tutorial

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every methods in interface are implicitly public whether you defined or not. And if you implements the interface, you should override the method in your class. In order to override the method, you need to follow the overriding rules.

And the reason for every methods in the interface are public, is explained by David in his post.

[When overriding a method, you can implement the method with less restriction. public is the least restricted access modifier, so you should implement with public modifier.]

Is it clear?
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:Every methods in interface are implicitly public whether you defined or not. And if you implements the interface, you should override the method in your class. In order to override the method, you need to follow the overriding rules.

And the reason for every methods in the interface are public, is explained by David in his post.

[When overriding a method, you can implement the method with less restriction. public is the least restricted access modifier, so you should implement with public modifier.]

Is it clear?


Thankyou very much abimaran for this explanation. I enjoy discussing on this forum
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
me too. I'm addict to this forum!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic