Test your knowledge with free interactive questions on Seneca — used by over 10 million students.

Encapsulation

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

Setters and getters

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.
Encapsulation

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.
Different instances

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
1

Components of a Computer

2

Software & Software Development

3

Exchanging Data

4

Data Types, Data Structures & Algorithms

5

Legal, Moral, Cultural & Ethical Issues

6

Elements of Computational Thinking

6.1

Thinking Abstractly

6.2

Thinking Ahead

6.3

Thinking Procedurally

6.4

Thinking Logically

6.5

Thinking Concurrently

7

Problem Solving & Programming

8

Algorithms

Practice questions on Encapsulation

Can you answer these? Test yourself with free interactive practice on Seneca — used by over 10 million students.

  1. 1
  2. 2
  3. 3
  4. 4
Answer all questions on Encapsulation

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