mercoledì 27 marzo 2013

Q13

Q13


Given the code fragment:
 


public interface SampleCloseable {
       public void close() throws java.io.IOException;
}

Which implementation is valid?
A.
public class Test implements SampleCloseable {
       public void close() throws java.io.IOException { // do something }
       }
}

B.
public class Test implements SampleCloseable {
       public void close() throws Exception { // do something }
       }
}
C.
public class Test implementations SampleCloseable {
       public void close() throws Exception { // do something }
       }
}
D.
public class Test extends SampleCloseable {
       public void close() throws java.io.IOException { // do something }
       }
}

Risultato : A
B non è giusta perchè l'eccezione è diversa.
C utilizza una parola chiave inesistente : implementation.
D utilizza la parola chiave extends che non si usa per le interfacce ma solo per le classi.

Si noti che B non funziona perchè Exception è più generica (alta) di IOException. Se fosse stato :

public interface SampleCloseable {
       public void close() throws Exception;
}
public class Test implements SampleCloseable {
    public void close() throws java.io.IOException { // do something }
    }
}
Il codice avrebbe compilato senza problemi.

 

Nessun commento:

Posta un commento