Get your CodeRanch badge!*
The moose likes Java in General and the fly likes Understanding interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Understanding interfaces" Watch "Understanding interfaces" New topic
Author

Understanding interfaces

Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

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 ?


Understanding is Everything - Peter Lord
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Because interface methods are intrinsically public, and the point of an interface is to define an external contract.
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
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

Joined: Jul 29, 2010
Posts: 121

this is the situation I wanted to know about




[/code]
Manjusha Muraleedas
Ranch Hand

Joined: Jun 02, 2010
Posts: 52
Why do we have to write public for boolean method


You have already got the answer from David Newton .

Manjusha Muraleedas
Ranch Hand

Joined: Jun 02, 2010
Posts: 52

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

Joined: Jul 29, 2010
Posts: 121

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

Joined: Jun 02, 2010
Posts: 52
you need to have a basic understanding of java interface.

The following tutorial may help you...Interface Tutorial

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

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

Joined: Nov 04, 2009
Posts: 2066

me too. I'm addict to this forum!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding interfaces
 
Similar Threads
Inner classes and interfaces?
difference between interface and abstract classes
Why Session bean methods throw RemoteException
interface example III (question)
Constructor in interface?