OOP – Encapsulation – Q&A

Question 1[2 marks]
class Padlock:
  private key
  private locked
  //Constructor for the Padlock class:
  procedure new(str_key,bool_locked):
    key = str_key       
    locked = bool_locked  
  public function unlock(combination):
    if combination==key:
       locked = False
       return True
      return False
  public procedure lock():
    locked = True
  public function getKey():
    return key
  public function setKey(combination):
    if length(combination)==4:
      key = combination
      return True
      return False
What code would you write to instantiate two new padlocks: One called bikeLock that is already locked with a combination of 4321 and one called gardenShedLock that is unlocked with a combination of 1234?
Question 2[4 marks]
Explain using an example from the above code for the Padlock class, what is meant by encapsulation.
Save / Share Answers as a link


The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.7 / 5. Vote count: 13

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , ,