2.4.11

Encapsulation

Test yourself

Encapsulation

Encapsulation in object-oriented programming is a principle that concerns how data inside the class can be accessed or changed.

Illustrative background for Setters and gettersIllustrative background for Setters and getters ?? "content

Setters and getters

  • A setter is a special type of method that sets one of the attributes equal to a specific value.
  • A getter is a special type of method that obtains the value of one of the attributes of the object.
Illustrative background for EncapsulationIllustrative background for Encapsulation ?? "content

Encapsulation

  • Each attribute can only be accessed or modified by methods that are set (encapsulated) when defining the class.
    • This is also known as 'information hiding'.
  • The use of these methods protects the data in the class instance from being tampered with.
Illustrative background for Different instancesIllustrative background for Different instances ?? "content

Different instances

  • An instance of a class should not be able to alter the data of another instance of the same class.
  • For example, if you have an instance of a car class called “car1”, its methods should not be able to alter the attributes of another instance of the car class that is also running.

Jump to other topics

1Components of a Computer

2Software & Software Development

3Exchanging Data

4Data Types, Data Structures & Algorithms

5Legal, Moral, Cultural & Ethical Issues

6Elements of Computational Thinking

6.1Thinking Abstractly

6.2Thinking Ahead

6.3Thinking Procedurally

6.4Thinking Logically

6.5Thinking Concurrently

7Problem Solving & Programming

8Algorithms

Unlock your full potential with Seneca Premium

  • Unlimited access to 10,000+ open-ended exam questions

  • Mini-mock exams based on your study history

  • Unlock 800+ premium courses & e-books

Get started with Seneca Premium