开发者

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 hav开发者_如何学Ce. Any help would be appreciated. Thanks

  public interface Intset {

  public abstract boolean isElem (int a)

}


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.


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

 public interface Intset {

  public abstract boolean isElem (int a);

}


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);
}


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜