0% found this document useful (0 votes)
126 views1 page

Object Oriented Programming

Access modifiers in object-oriented programming languages restrict access to classes, methods, and attributes. The four access modifiers in Java are public, protected, package-private, and private. Public access has no restrictions, protected allows access within packages and subclasses, package-private allows access within the same package only, and private allows access only within the defining class. To ensure only one Preference object exists in a running game, the Singleton design pattern can be applied by making the constructor private and providing a public static method to lazily instantiate and return the sole instance. It is not necessary to make the class final or implement Cloneable. Inheriting singleton behavior from a base class is not viable because the subclass would not

Uploaded by

Edgar Huanca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views1 page

Object Oriented Programming

Access modifiers in object-oriented programming languages restrict access to classes, methods, and attributes. The four access modifiers in Java are public, protected, package-private, and private. Public access has no restrictions, protected allows access within packages and subclasses, package-private allows access within the same package only, and private allows access only within the defining class. To ensure only one Preference object exists in a running game, the Singleton design pattern can be applied by making the constructor private and providing a public static method to lazily instantiate and return the sole instance. It is not necessary to make the class final or implement Cloneable. Inheriting singleton behavior from a base class is not viable because the subclass would not

Uploaded by

Edgar Huanca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

COMPUTER SCIENCE TRIPOS Part IA 2014 Paper 1

Object-Oriented Programming (RKH)


(a) (i ) Explain the purpose of access modifiers in OOP languages.

[2 marks]

(ii ) Copy and complete the table below to show the access restrictions for the
four access modifiers in Java.
[2 marks]
Access Modifier
Defining class
Class in same package
Subclass in different package
Non-subclass in different package
(b) A Java game designer wishes to store all the game preferences (e.g., player name,
screen size, music volume, etc.) within a custom Preference class.
(i ) Assuming each preference is stored as a unique String key mapping to
a String value, give a simple implementation of Preference that allows
for efficiently setting or updating preferences and retrieving previously set
ones. Your implementation should define an exception that is thrown when
a preference key is requested but not present.
[5 marks]
(ii ) It is important that only one Preference object exists in a running game.
Show how to apply access modifiers and the Singleton design pattern to
ensure this. Your implementation should lazily instantiate the object. Is it
necessary to make your class final or Cloneable? Explain your answer.
[6 marks]
(c) The designer also implements other Singleton classes in the game and proposes
to create a SingletonBase base class from which all such classes would inherit
the singleton behaviour. By providing example Java code, explain why this is
not viable.
[5 marks]

You might also like