Do we need to declare interface methods as abstract [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java abstract interface
public interface Foo {
abstract public void bar();
}
I guess we don't need to declare abstract as well as public in the above interface. Will the compiler catch this is as a warning or is it allowed by the compiler.

In an interface the the modifiers public and abstract are implied for methods, similarly for fields public static and final are implied. For inner classes static is implied.

It is allowed. public and abstract are automatically added to every interface method.

You don't have to, every interface method is implicitly abstract. It will not be a mistake to write it though.

For interface methods, it is not necessary to declare public and abstract
by default those are public and abstract

It is not necessary but it won't hurt to write it. These modifiers are implied.
I like to do it so everything is explicit and may help other programmers that will work with your code.

You are allowed to declare abstract inside the interface. The complier can pass it.
public interface foointerface {
abstract public void foo();
public void bar();
}
But there is no point to declare in abstract since
we would not implement or allow to implement methods inside interface.

Related

Why should I declare implemented interface methods as "public"?

interface Rideable {
String getGait();
}
public class Camel implements Rideable {
int weight = 2;
String getGait() {
return " mph, lope";
}
void go(int speed) {++speed;
weight++;
int walkrate = speed * weight;
System.out.print(walkrate + getGait());
}
public static void main(String[] args) {
new Camel().go(8);
}
}
Upon compiling the above code I've got a compilation error, related to access modifier of getGait() method. Please explain, why should I declare getGait() with public access modifier?
getGait() of Camel implements a method of the Rideable interface. All interface methods are public by default (even if you don't specify it explicitly in the interface definition), so all implementing methods must be public too, since you can't reduce the visibility of the interface method.
In the interface you have method getGait() declared as public. Even you do not state a method as a public in interface it is public.
But in your class, you have declared this method as package private. It is not allowed, because it reduces the visibility of the implemented method.
To avoid this problem. either declare this method as public in your class, or remove declaration (implements Rideable) that your class implements the interface with this method signature.
According object oriented fundamentals, interface contains only public methods. so when you implements interface, you should declare it as a public , otherwise it give you compile time error.
Thanks.
One of the basic usage of interfaces can be to check conformance.For example a class implementing Comparable interface must provide compareTo method and hence providing a mechanism to compare objects of the class.
The reason these methods being public makes sense is that any class which utilizes this conformance must be able to use these methods without any restriction.For example sort method of Arrays class will be good enough to sort objects of a class only if it implements Comparable and exposes the compareTo method(If thats the mechanism you want to provide for sorting.Of course Comparator is also there). So in a nutshell, a contract is only good enough if its readable or usable in case of interfaces(thus making methods public imperative).
In Interface, the fields are implicitly public static final and the methods in an interface are by default public.
Please read the rules of Inheritance:
http://www.codejava.net/java-core/the-java-language/12-rules-of-overriding-in-java-you-should-know
One of which says, "The overriding method must not have more restrictive access modifier". So you are ovveriding the getGait() in Camel class. If you do not provide the access modifier in methods of class then by defaults its default. Which mean you are restricting the access modifier from public to default. Hence breaking the rule of ovveriding which is why its complaining.

Difference between using an abstract keyword for a method in interface vs not using the abstract keyword [duplicate]

This question already has answers here:
Why would one declare a Java interface method as abstract?
(4 answers)
Closed 6 years ago.
Say I have a Interface FirstInterface as follows :
public interface FirstInterface {
public void myInterfaceMethod();
}
Here I am declaring a method myInterfaceMethod() which will be defined in the class that will implement this Interface.
but I can also do
public interface FirstInterface {
public abstract void myInterfaceMethod();
}
I have added a keyword abstract in the method declaration.
I want to know, if at all, does it make any difference to add a abstract keyword in the method declaration or not?
All non-default, non-static methods in interfaces are abstract by default. Adding the keyword is harmless, but changes nothing and is discouraged by the JLS "as a matter of style."
From JLS§9.4:
An interface method lacking a default modifier or a static modifier is implicitly abstract... It is permitted, but discouraged as a matter of style, to redundantly specify the abstract modifier for such a method declaration.

What's the difference between public method and abstract method in Interface?

Consider the following interface :
public interface MyInterface {
public void func1();
public void func2();
abstract public void func3();
}
and the class MyClass :
public class MyClass implements MyInterface{
#Override
public void func1() {
// TODO Auto-generated method stub
}
#Override
public void func2() {
// TODO Auto-generated method stub
}
#Override
public void func3() {
// TODO Auto-generated method stub
}
}
What's the difference between func2() and func3() ?
I must implement both when I want to implement MyClass , so it seems that there's no
difference if I write public or abstract public in the interface .
Thanks
Java 7 and earlier:
There is no difference since all interface methods are public and "abstract." This is implied whether declared or not.
Java 8:
The same rules apply as in Java 7, however, it should be noted that since Java 8, only non-default methods are "abstract." Default methods, are in fact, allowed to have an implementation.
Java 9:
In Java 9, we are provided even more flexibility, and also allowed to have private methods.
Java Language Specification quote:
Every method declaration in the body of an interface is implicitly
abstract, so its body is always represented by a semicolon, not a
block.
You ask
What's the difference between func2() and func3() ?
Except for the fact that they are different methods, their modifiers are the same.
What's the difference between public method and abstract method in Interface?
9.1.1.1. abstract Interfaces
Simply said... none. According to this, it's obsolete.
In a way, we are comparing apples and oranges in this question. For someone new to the Java language, some clarifications need to be made first:
Interfaces:
Interfaces contain method stubs. These are methods without a body.
These methods are implicitly "abstract" and we do not need to explicitly use a modifier to denote this.
That being said, since Java 8, we now have what are called "default methods." These methods are fully implemented methods which do require an explicit declaration using the default keyword.
That being said, the abstract keyword never comes into play in the context of an interface.
Abstract Methods:
Abstract methods only belong in abstract classes.
An abstract class may contain fully implemented methods along with abstract methods. However, just one abstract method in a class is enough to make it an abstract class - That is, the class must be explicitly declared as an abstract class.
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

Why should we declare interface methods as public? [duplicate]

This question already has answers here:
Protected in Interfaces
(15 answers)
Closed 5 years ago.
When I implement an interface method, I am forced to make it a public method.
We may have cases where we want to use either the default (like in case of access within the same package) or protected.
Can anyone please explain the reason behind this limitation?
Interfaces are meant to define the public API of a type - and only that, not its implementation. So any method (or static member) you define in an interface is by definition public.
Since an interface can't contain any concrete implementation, there is no way to call any member methods from within. And declaring such methods but leaving the calls to them to subclasses or totally unrelated clients would mean your type definition is incomplete and brittle. That is why if you need to define protected or package access members, you can do so in an abstract class (which may also contain implementation).
Maybe this will provide some answers.
To my knowledge, you use interfaces to allow people from outside your code to interact with your code. To do this, you need to define your methods public.
If you would like to force someone to override a given set of private methods, you might want to declare an abstract class with a series of abstract protected methods.
An interface is a contract that the class that implements it will have the methods in the interface. The interface is used to show the rest of the program that this class has the methods and that they could be called
EDIT: This answer is meant for C# interface implementations. In this case of Java the scenario is similar just that the syntactic analyzer wants a public keyword mentioned in the interface, which is implicitly done in C#
Interface methods are implicitly public in C# because an interface is a contract meant to be used by other classes. In addition, you must declare these methods to be public, and not static, when you implement the interface.
interface IStorable
{
void Read( );
void Write(object obj);
}
Notice that the IStorable method declarations for Read( ) and Write( ) do not include access modifiers (public, protected ..). In fact, providing an access modifier generates a compile error.
class Document : IStorable
{
public void Read( )
{
//
}
public void Write(object obj)
{
//
}
}
Just think about interfaces as Contracts to be implemented as public
If we mark a interface method as private the implementing class wont
see the method and cant override it.
If we mark a interface method as protected the implementing class
wont see the method unless it is in the same package as the
interface.
If we mark a interface method without any access modifier the
implementing class wont see the method unless it is in the same
package as the interface

what is a abstract method on a interface in java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why would one declare a Java interface method as abstract?
I found the following code in one of our ejb interfaces. Does anyone know what the abstract does in the interface? If you do please also explain why it might be needed or provide a reference to read about it =)
#Local
public interface IDomasOrderProcessor {
public abstract void executeOrderLines(List<OrderLine> lines);
public abstract void setupJob(List<OrderLine> lines);
public abstract void setupJob(OrderLine line);
}
abstract is redundant in this case. All methods defined on an interface are public and abstract by definition.
Excerpt Java Language Specification section 9.4
Every method declaration in the body of an interface is implicitly
abstract, so its body is always represented by a semicolon, not a
block.
Every method declaration in the body of an interface is implicitly
public.
Both public and abstract modifiers are implicit in interfaces and should be avoided.
A method in an interface is public and abstract by definition. I have heard some people say they feel that explicitly declaring them like that makes it clearer, but to me it seems like extra noise.
As per this
document all the methods of interface is public and abstract, so there is no mean to define explicitly abstract method inside the interface.

Categories

Resources