0

I'm struggling with the following task:

Create a class Person which has at least the attribute name. When created the object is in read-only mode (only getters are allowed). By using the method unlock, which requires a password, the object goes in read/write state. It is important, that the compiler checks if the object is used correctly (depending on the current mode). Do not use flags A read only object must not be copyable.

I think I can do the last one, but have no idea how to do the compiler-checks. Any hints?

Georgi Georgiev
  • 3,854
  • 5
  • 29
  • 39
  • "current" mode would only be known/changed at runtime. so, how's the compiler supposed to check it? – Ravi K Thapliyal Jun 22 '13 at 17:38
  • this is some homework assignment, isn't it? why did you add the `generics` tag. you question doesn't mention generics. is your exercise about generics? ;-) – user829755 Jun 22 '13 at 17:52
  • @user829755 It is a task form last year OOP exam. I have an exam next week so I try to solve old ones, I'm not sure if have to use generics I guess I need them for the compiler checks. – Georgi Georgiev Jun 22 '13 at 18:05

1 Answers1

1

generics would be helpful like this:

class InternalPerson extends PublicPerson which has name as a field. InternalPerson has a setter for the name but PublicPerson has only the getter. normally you operate on PublicPerson but the unlock method gives you the InternalPerson object.

not really safe since you can always cast PublicPerson to InternalPerson but maybe good enough for the exam?

user829755
  • 1,489
  • 13
  • 27