1

Possible Duplicate:
Why would one declare a Java interface method as abstract?

The following code snippet defines an abstract interface and does the sum of two numbers entered through the console.

package abstractinterface;

import java.util.Scanner;

abstract interface SumInterface
{
    abstract public void sum();
}

final class Summation implements SumInterface
{
    private int x,y;

    public Summation(int x, int y)
    {
        this.x=x;
        this.y=y;
    }

    public void sum()
    {
        System.out.print("\nSummation = "+(x+y)+"\n\n");
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        try
        {
            Scanner s=new Scanner(System.in);

            System.out.print("\nEnter a number:->");
            int x=s.nextInt();

            System.out.print("\nEnter another number:->");
            int y=s.nextInt();

            new Summation(x, y).sum();
        }
        catch(NumberFormatException ex)
        {
            System.err.println(ex.getMessage());
        }
    }
}

The interface contains one abstract method and it is implemented by the class Summation. The only question here is that if the keyword abstract is removed from the above interface, does it make some different sense? What is the actual use of such abstract interfaces in Java?

Community
  • 1
  • 1
Lion
  • 18,729
  • 22
  • 80
  • 110

4 Answers4

8

Its usage is marked "obsolete and should not be used" in JLS 9.1.1.1, abstract Interfaces.

IMO it's also misleading, because of its redundancy--it implies something different than an interface that isn't marked abstract, but all interfaces are implicitly abstract.

This SO question discusses a bit further.

Community
  • 1
  • 1
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
2

abstract is optional for interfaces. I would use it if your believe it makes things clear.

For methods of an interface public and abstract are optional.

For constants of an interface public static and final are optional.

For classes and interfaces in an interface public and static are optional.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

The keyword abstract is unnecessary when you use an interface. All methods in a Java interface are abstract. I would go beyond "unnecessary" and ask you to remove abstract from your interface declaration if this was a code review.

The abstract keyword applies to classes; it's used to distinguish methods that are abstract from those that have default implementations.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    Well, you can also decree a class to be abstract without it having any abstract methods... having abstract methods automatically forces the class to be abstract, but the converse isn't necessary. (On the other hand, if you think of an `interface` as a class with only abstract (public) methods and no non-staticonst members, then it's clear that the `abstract` class modifier is redundant on interfaces.) – Kerrek SB Nov 12 '11 at 15:12
  • Yes, all correct. You can have an abstract class with no abstract methods, but it would make me question the need for "abstract class". Sounds concrete to me. All of them have sensible default behavior in that case. The one reason to retain the keyword would be to make clear your intention that the class never be instantiated on its own. – duffymo Nov 12 '11 at 15:15
  • 1
    Exactly. This is just the separation of semantics and implementation. The semantics declare what a class *means*, and it is (and should be) possible to declare your intention of abstractness, independent of whether a concrete implementation of the class *could* physically be used on its own or not. It's a different matter whether this is *useful* in practice, but from a pure language-design point of view it makes sense to separate these concepts. – Kerrek SB Nov 12 '11 at 15:19
1

There is no difference. Java compiler does an auto-code generation (in the same way as if your class doesn't have any constructors, but by default it has <ClassName>(){} constructor).

Java interface syntax:

[abstract] interface <Interface Name> {
[public abstract] <type> <Method Name>(<formal arguments>);
}

[<content>] means that it does not matter write or not a content.

itun
  • 3,439
  • 12
  • 51
  • 75