Why interface method scope is only public? [duplicate] - java

This question already has answers here:
Protected in Interfaces
(15 answers)
Closed 9 years ago.
In interfaces why method access specifier is only public why not protected?
interface IPractice {
void test(); // it will be public
protected void test2(); // why this is not allowed
}
Can any one explain me this.

The whole point of an interface is that it exposes methods to the outside world so implementation details can be hidden.
What happens inside the interface should not be known to the outside world.

Because an Interface is public by nature, if you declare an Interface is because you want to make sure that everyone implements the same methods and such methods are PUBLIC.
Think of an interface as the controls of a car (Steering Wheel, Brake, Clutch...) no matter what, that is ALWAYS visible.
If you want several classes to implement the same method but make it protected, you may want to consider an abstract class instead.

An Interface is used to access the functionality of the class which is implementing it so you can assign object of class to the Interface reference. And you can call methods from that reference. So only public functionality can be accessed.

Related

A query regarding abstract class in java [duplicate]

This question already has answers here:
Is it possible to hide or lower access to Inherited Methods in Java?
(8 answers)
Closed 5 years ago.
I am writing a program where there are quite a lot number of methods in a particular class, so I decided to write an abstract class to help in keeping track of the methods.
Now, consider this: I have declared a method as abstract in the abstract class, and in the other class (which extends the abstract class), I want to override this method, but with access privilege reduced to private. Here, the compiler is giving a problem. It says that an attempt to assign weaker access privileges is being met with, which cannot be allowed. If I try to declare the method in the abstract class as protected (I have also changed the private ones to protected in the sub-class), it says that modifiers abstract and protected cannot be used together.
So, what can I do? Do I have to make the methods package access or public in both classes? If so, is there no way that I can declare these methods private?
Please note that I'm asking only for abstract classes, and not all classes in general.
What do you mean it can not be protected abstract - of course it can.
And the thing that you want to do is basically prohibited by the compiler and the language itself in the first place.
The answer to your question is: there's nothing you can do to reduce the visibility of a method declared in a parent class.
If you can restate what you're trying to accomplish by "keeping track of the methods" in an abstract parent class, you might get a different solution.

What is the use of abstract class that implements an interface [duplicate]

This question already has answers here:
Why an abstract class implementing an interface can miss the declaration/implementation of one of the interface's methods?
(7 answers)
Closed 7 years ago.
While reading Herbert Schildt I came across partial implementation where overriding is'nt mandatory , But I fail to understand why do we implement such an interface where we don't override its methods :
interface CallBack{
void callback();
}
abstract class Incomplete implements Callback { //Legal
void someMethod();
}
Is there any practical use of such a class or it's just theoretical ?
One use case is a family of classes which all have to implement the callback interface in the same way. So it could be implemented in the abstract superclass and you don't have to handle it in every subclass.
Abstracted classes can't be instantiated, so you would make sure that all subclasses that you will instantiate in you system later handle the callbacks in the same way.
The name it self showing that it's abstract, it need not to implement. Where as the subclasses of that abstract class must and must full fill that definition and needs to implement that methods in that interface.
Every subclass of Incomplete now have to implement Callback.

Differences on Interface implementation [duplicate]

This question already has answers here:
Should methods in a Java interface be declared with or without a public access modifier?
(12 answers)
Closed 8 years ago.
I have maybe a simple question.
Here two code-snippets to show what I mean:
Example 1:
public interface SomeInterface{
public void someMethod(...);
}
Example 2:
public interface AnotherInterface{
void anotherMethod(...);
}
So, Example 1 is completely clear to me but Example 2 isnt.
In fact, is there any difference between those two examples expect the public-modifier?
On one hand I found that methods from Interfaces are implicitly public but on the other hand I have found that methods declared in an Interface are "package-public" (I dont now if thats the correct description) - saying these are visible to all classes in the same package as the Interface.
For now I am completely confused.. So could someone please explain me whats right?
Thanks anyways.
It's redundant to declare it public. In particular JLS 9.4 states:
Every method declaration in the body of an interface is implicitly public (ยง6.6).
Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.
It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface.
All interface methods are public abstract and all interface fields are public static final.
So there is no difference in above examples.
All methods in an interface are public and visible to implementing classes everywhere. But if the interface itself is package local (has no modifier - the default), then those methods are visible only to classes/interfaces within the same package. But the method would still have to be public in the implementing class
In the code you have above, there is no difference. But if it had been:
interface AnotherInterface{ // Note no modifier - default modifier applied
void anotherMethod(...);
}
In this case, the interface is visible only inside the same package.
NOTE: It's the interface itself that can be package-private, not the methods in it. You can define an interface that can only be used (by name) within the package it's defined in, but its methods are public like all interface methods. If a class implements that interface, the methods it defines must be public. The key thing here is that it's the interface type that isn't visible outside the package, not the methods.

What is the need to use Interface in java? [duplicate]

This question already has answers here:
what is the actual use of interface in java? [duplicate]
(6 answers)
Closed 9 years ago.
I am just in little bit confusion about for what interface used in Java.
For example, I am creating an interface like,
interface Inter
{
void get();
}
And I am implementing it in a class like,
class Base implements Inter
{
void get()
{
------
}
}
Whats the difference between, if I declare a class like
class Base
{
void get()
{
------
}
}
Is there any difference in it? then why should I use interface in java. I know its a basic question. but I am in confusion. So please solve this..
An interface makes the contract between caller and called classes more explicit.
Also, it allows you to mimic multiple inheritance to a certain extent (say you
have super class A and you cannot change it, you can still implement some
interface B and pass your class to a method which accepts B as parameter
but knows nothing about your super-class A).
public interface Animal {
public void eat();
public void sleep();
}
public class Dog implements Animal{
// Now FOR A DOG TO BE AN ANIMAL, IT MUST IMPLEMENT ALL THE BEHAVIOURS(METHODS) OF ANIMAL INTERFACE. IF IT MISSES EVEN ONE BEHAVIOUR, THEN A DOG IS NOT AN ANIMAL.
public void eat()
{
}
public void sleep()
{
}
}
Why Interfaces?
An interface is a contract or a protocol, or a common understanding of what the classes can do. When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface. Interface defines a set of common behaviors. The classes implement the interface agree to these behaviors and provide their own implementation to the behaviors. This allows you to program at the interface, instead of the actual implementation.
One of the main usage of interface is provide a communication contract between two objects. If you know a class implements an interface, then you know that class contains concrete implementations of the methods declared in that interface, and you are guaranteed to be able to invoke these methods safely. In other words, two objects can communicate based on the contract defined in the interface, instead of their specific implementation.
First, it can be usefull because you can't herit from multiple class.
And then it is a kind of contract also.
All classes which implements Movable will be able to move, but each one with his own way.
So when you handle a Movable, no matter his class, you know that you can use the method move().
Hope it helps.

Sub-Protected Method in java. (Can't think of a better way to phrase this) [duplicate]

This question already has answers here:
how to restrict protected method access to only subclasses
(2 answers)
Closed 8 years ago.
How could I create a method in a given class that can only be called internally by it, and its subclasses.
For example, the class Foo:
public class Foo{
public Foo(){
}
???? void bar(){
}
}
and the class Baz, extending Foo:
public class Baz extends Foo{
public Baz(){
this.bar(); //valid
}
}
but also, some other random class, Qux:
public class Qux{
private Baz baz = new Baz();
public Qux(){
baz.bar(); //invalid
}
}
note that any of these classes could be in any package, so a simple 'protected' wont work.
if this isn't possible with a keyword or something similar, i'm open to suggestions on how to achieve similar behavior.
There is no visibility level you can use. The levels are strictly nested in Java, and a level that would give access to subclasses but not the package would break that.
In Java 1.0 there was an access level "private protected" that did just this, and it was removed from later versions because it complicated the language and wasn't very useful: A subclass may be created by someone you don't trust, while code in the same package is controlled by you. Therefore it's more important to protect yourself from subclasses.
This is not really possible with an access modifier. As you have only two choices between protected modifier (subclasses + classes in the same package) and default modifier (classes in the same package).
The only way you can achieve this by removing all non-child classes to other packages with protected/default access specifier.
public is the only keyword that allow to call a method from non-extending class in a different package. So if you still want to hide the method from pulic view and put your class anywhere, you will have to invoke the method via reflection (which is dirty).

Categories