public class JavaSource {
public static abstract int getCertified();
public abstract int getCertified();
public final abstract int getCertified();
final abstract int getCertified();
public int abstract getCertified() {};
}
a) Only one method needs to be commented out or removed
b) Two methods needs to be commented out or removed
c) Three methods needs to be commented out or removed
d) Four methods needs to be commented out or removed
e) All Five methods needs to be commented out or removed
In fact, only option e), all five methods must be commented out or removed, is correct.
A method cannot be both abstract and final, so the first method would trigger the following compiler error:
JavaSource.java:4: illegal combination of modifiers: abstract and static public static abstract int getCertified();
The second method properly defines an abstract method, but since the class itself is not abstract, the compiler will generate the following error message:
JavaSource.java:1: JavaSource is not abstract and does not override abstract method getCertified() in JavaSource
public class JavaSource {
^
1 error
Remember, any class with an abstract method must also declare itself, in the class declaration, to be abstract as well.
The third method triggers the following compile error, due to the fact that is used both the abstract and final keywords together in the method declaration.
C:\_jdk1.6\bin>javac JavaSource.java
JavaSource.java:6: illegal combination of modifiers: abstract and final
public final abstract int getCertified();
^
JavaSource.java:1: JavaSource is not abstract and does not override abstract met
hod getCertified() in JavaSource
public class JavaSource {
^
2 errors
Remember, the keywords abstract are totally opposite to each other when it comes to describing methods. An abstract method MUST be overridden by a concrete, implementing or extending class, while a final method CANNOT be overridden by any subclass whatsoever. A method cannot be decorated with the keyword abstract AND final at the same time.
Similarly to the third method, the fourth method uses abstract and final at the same time, and as a result, it will not compile.
The fifth method will not compile for a variety of reasons. First of all, the order of the abstract keywork and the int data type are transposed. The abstract keyword must come before the data type declaration. This causes the following error to be produced by the Java compiler:
C:\_jdk1.6\bin>javac JavaSource.java
JavaSource.java:8:
public int abstract getCertified() {};
^
JavaSource.java:8: invalid method declaration; return type required
public int abstract getCertified() {};
^
2 errors
Furthermore, the fifth method will also not compile due to the fact that it is marked abstract, while at the same time, providing those braces after the method declaration. Methods being described as abstract cannot have open and closed, curly parenthesis after them, otherwise, the JVM thinks a method is being implemented, which is completely contrary to the idea of an abstract method. As a result, the fifth method in this example would generate the following compile error:
C:\_jdk1.6\bin>javac JavaSource.java
JavaSource.java:1: JavaSource is not abstract and does not override abstract met
hod getCertified() in JavaSource
public class JavaSource {
^
JavaSource.java:8: abstract methods cannot have a body
public abstract int getCertified() {return 0;};
^
2 errors
Remember, abstract methods cannot have a body defined in them at all. Methods marked as being abstract are not allowed to have any method implementation at all.
To get this class to compile, with it containing a single abstract method, it would need to look something like this:
public abstract class JavaSource {
//public static abstract int getCertified();
//public abstract int getCertified();
//public final abstract int getCertified();
//final abstract int getCertified();
public abstract int getCertified();
}
No comments:
Post a Comment