
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

COMPUTER SCIENCE TRIPOS Part IA โ 2018 โ Paper 1
Object-Oriented Programming (RKH)
Consider the following Java class.
1 public class Child extends Parent { 2 3 public int x=0; 4 5 public boolean equals(Object obj) { 6 if (this == obj) return true; 7 if (!super.equals(obj)) return false; 8 if (getClass() != obj.getClass()) 9 return false; 10 Child other = (Child) obj; 11 if (x != other.x) return false; 12 return true; 13 } 14 }
(a) Explain why it is bad practice for member variables such as x to be public. [2 marks]
(b) For each of the lines 6โ12, explain its purpose. Illustrate your answer with examples. [7 marks]
(c) What could result from a call to c.equals(null), assuming c is a reference to a Child object? [3 marks]
(d ) What would be the consequences of replacing the equals signature with public boolean equals(Child obj)? [4 marks]
(e) Explain why Child should also override the hashCode() method. [4 marks]