Class Student{…} Class ResidentStudent extends Student {… public void setPremium
ID: 3904103 • Letter: C
Question
Class Student{…}
Class ResidentStudent extends Student {…
public void setPremiumRate(double pr){…}
}
class NonResidentStudent extends Student {…
public void setDiscountRate(double dr) {…}
}
Studdent s1 = new Student();
Studdent s2 = new ResidentStudent();
Studdent s3 = new NonResidentStudent();
((ResidentStudent) s3).setPremiumRate(1.25); //This may fail if s3 were not of type ResidentStudent (i.e if it was a NonResidentStudent which does not support setPremiumRate()).
Write an if statement below to keep the call from executing if the type of s3 is not ResidentStudent.
If( ){
((ResidentStudent) s3).setPremiumRate(1.25);
}
Explanation / Answer
In order to keep the call from executing if the type of s3 is not ResidentStudent you can basically use the functionality of instance of operator
((ResidentStudent) s3).setPremiumRate(1.25);
}