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 ?
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.
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%|
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