bob
bob

Reputation: 49

Interface implementation

I have been asked the following question:

a) The interface IntSet has a single method called isElem. The method takes a single parameter of type int and returns a boolean result. Define the interface IntSet in full.

So far for my answer I have. Any help would be appreciated. Thanks

  public interface Intset {

  public abstract boolean isElem (int a)

}

Upvotes: 0

Views: 192

Answers (5)

user4060156
user4060156

Reputation:

you forgot to write ; and in interface methods are by default pulbic and abstract so

you can write

public interface IntSet{
    boolean isElem(int val);
}

or

public interface IntSet{
    public abstract boolean isElem(int val);
}

Upvotes: 1

Tom Tresansky
Tom Tresansky

Reputation: 19892

Since every method in an interface is by default public and abstract

public interface IntSet {
  boolean isElem (int a);
}

I would drop the public abstract from the code. You rarely see this, since it is redundant.

From the 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.

and the grammar:

InterfaceMemberDeclaration:
    ConstantDeclaration
    AbstractMethodDeclaration
    ClassDeclaration 
    InterfaceDeclaration
    ;

also demonstrates all methods declared in an interface are abstract.

Upvotes: 0

RoflcoptrException
RoflcoptrException

Reputation: 52229

The correct solution is:

public interface IntSet {

  public abstract boolean isElem (int a);

}

You forgot the ; at the end of the method definition, and you had a small typo in the class name.

Note that the keywords public and abstract are optional and discouraged in this case.

Upvotes: 4

Steve G.
Steve G.

Reputation: 598

public interface IntSet{
    bool isElem(int val);
}

Upvotes: -1

drei01
drei01

Reputation: 307

apart from the trailing semi-colon, you have completed the question.

 public interface Intset {

  public abstract boolean isElem (int a);

}

Upvotes: 1

Related Questions