1.3 Method overriding refers to an instance method in a subclass that has the same method signature and return type of a method in the superclass. The idea is to replace the implementation of the method in the superclass by the method defined in the subclass. The method toString() in PostGraduate is an overridden method of toString() in the Student class. [3]
1.4 It is only a good idea when it the class acts just as a template from which other classes inherit from and does not make any sense to create objects of Student – this arises when you have a specialised classes to represent every kind of student . [2]
Question 2 [12]
2.1 The assertion itemsInBin>0 is false and the program aborts and the last line never executes. [1]
2.2
Language: Java
public void removeFromBin() throws Exception
{
if (itemsInBin == 0)
throw new Exception("Stock is Finished");
else{
--itemsInBin;
}
}
[4]
2.3
Language: Java
public static void main(String[] args) {
try{
StockBin b = new StockBin(1);
b.removeFromBin();
b.removeFromBin();
}
catch (Exception e) {
System.out.println("Stock Finished"+e);
}
System.out.println("Finished with Stock Control" );
}
[5]
2.4 Assertion should not be used to replace exception handling. Exception handling deals with unusual circumstances during program execution. Assertions are to assure the correctness of the program. Exception handling addresses robustness and assertion addresses correctness. Like exception handling, assertions are not used for normal tests, but for internal consistency and validity checks. Assertions are checked at runtime and can be turned on or off at start up time. [2]