Discussion Locked

abstract Interface

0 helpful
11 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi friends,

Can anyone tell me what is the logic behind declaring an Interface as abstract in Java.

e.g. public abstract interface Statement

Nise

11 replies

by jay eckles

An abstract interface has one or more abstract methods that must be defined in any class that implements the interface.

For instance, if I had an interface "Cookable" with a method "cook()", and classes "Toast", "HotDog", and "Cake" that implement Cookable, it wouldn't make sense to try to define or implement cook() in the Cookable interface. Each class that implements Cookable will need to define cook() in a different way. Thus cook() should be declared abstract, essentially deferring its definition until the interface is implemented by another class.

Hope that explanation helps a little.

Jay Eckles
www.jayeckles.com

by nise_k

Hi Jay,

As far as I know, Interfaces are made so as to force the implementing classes to include the code for the methods declared inside the Interface. There seems no valid reason, y again an Interface is declared Abstract.

Nise

by okujava

It seems to me, that the abstract keyword is only the marker to show, that "it is abstract". just as the methods in interfaces can be marked abstract, but must not, or the finals can be marked public static final, but you can ommit these keywords (the functionality is the same).

cheers,

by java guru

I thought there were abstract classes and interfaces, but no abstract interface.

An abstract class is a class that may has one or more methods that are declared abstract. Declaring a method abstract requires that method to be implemented by the EXTENDing class. Any methods that are declared in the abstract class that are not declared abstract methods are inherited like a normal superclass.

An interface defines methods that must defined, there is no code. Basically, it defines a signature contract that some other object will expect your object to expose.

What would be the point of an abstract interface? An interface by it's nature says you have to define the methods with the given signature. An abstract method defines a method that must be implemented. Declaring an interface as abstract would be redundant.

See http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html for an article about "Abstract classes vs. interfaces".

by bayard-generationjava

abstract interface is the 'correct' way to declare an interface.

A quick example interface:

public abstract interface Example {
public abstract void showExample();
}

However, the abstracts are unnecessary. By convention, and somewherein one of the Sun specs it advises against including abstract, developers do not mention the abstract keyword in an interface.

It is a given.

The public keyword is also unnecessary. The same spec suggests not using public, but a large number of us do. It's a habit.

In truth the interface should be:

public interface Foo {
void showExample();
}

As to that first public keyword. I don't know. I'd have to play to decide it a protected interface is legal or not. I see no reason why not, but logic and Java don't always mix.

by richard_hulse

Abstract suggests a non-implemented method. It forces the method to be implemented in a subclass. An interface provides limited inheritance since a class can only extend a single class but implement several interfaces. Thus an abstract interface allows inheritance to a subclass withour regard to what class it extends and also forces the implementation of a specific method(s).

This discussion is closed to new replies.