Online · Puchong · Selangor

mBlock Artificial Intelligence Courses for Children in Puchong

mBlock artificial intelligence courses for children in Puchong use visual blocks and AI extensions so students explore face tracking, voice-style controls and simple models—optionally linked to robot hardware later.

mBlock AI Puchong

1. Introduction: Connecting Encapsulation to Access Methods

In the study of data encapsulation, an object's internal data is hidden from the outside world by declaring variables as private using double underscores (__). Because the outside program cannot directly read or modify private variables, the class provides dedicated public methods through which external code can safely interact with that private data.

In object-oriented programming, these specialized access methods are known as Getters and Setters:

  • Getters: Functions designed to retrieve (get) the value of a class's internal data attributes.
  • Setters: Functions designed to assign or update (set) the value of a class's internal data attributes.

Whenever a private data member is declared in a class, best practice is to pair it with corresponding getter and setter methods to control external access.

2. Why Setters Are Essential: Sanity Checks & Data Validation

The true value of a setter method becomes clear when inspecting its body. If users could modify attributes directly, invalid or illogical data could enter the system.

A setter introduces validation logic (sanity checks) before assigning values to private members:

  • Logical boundaries: A student's grade must fall between 0 and 100. Even if a student claims to have given 110% effort, the maximum allowable grade remains 100.
  • Preventing negative values: If a user enters a negative grade (such as -20 or -95), whether accidentally or intentionally, the setter rejects it because a negative grade is invalid.
  • Protecting class ownership and quality: One of OOP's primary goals is code reusability. When you publish a class for other developers to instantiate and use in their applications, your class represents your work. If your class allowed an object to store -5 as a grade, the class design itself would appear flawed. Setters protect the class from accepting illogical values.

3. Implementation Walkthrough in Jupyter Notebook: The Student Class

The following implementation defines a Student class with a private __grade attribute, a getter (get_grade), and a setter with validation (set_grade):

class Student:
    # 1. Constructor initializing name and private grade
    def __init__(self, name, grade):
        self.name = name
        self.__grade = grade  # Private data member prefixed with '__'

    # 2. Getter method to retrieve the private grade
    def get_grade(self):
        return self.__grade

    # 3. Setter method with validation (sanity check)
    def set_grade(self, new_grade):
        if 0 <= new_grade <= 100:
            self.__grade = new_grade
        else:
            print(f"Invalid grade: {new_grade}. Grade must be between 0 and 100.")

4. Testing Valid Updates and Retrievals

When an object is instantiated and updated with valid data:

# 1. Initializing student1 with name "Ali" and initial grade 90
student1 = Student("Ali", 90)

# 2. Modifying grade to 95 using the setter method
student1.set_grade(95)

# 3. Retrieving and printing the updated grade via the getter
print("Updated Grade:", student1.get_grade())

Execution Walkthrough

  • The constructor initializes student1 with a grade of 90.
  • Calling student1.set_grade(95) passes 95 to the setter. Because 95 is between 0 and 100, the validation check passes, and self.__grade updates to 95.
  • Calling student1.get_grade() returns the updated value.

Output:

Updated Grade: 95

5. Testing Invalid Inputs and Data Protection

If an external user attempts to pass an invalid value, such as a negative grade:

# Attempting to assign an invalid grade of -95
student1.set_grade(-95)

# Checking the grade value using the getter
print("Current Stored Grade:", student1.get_grade())

Execution Walkthrough

  • When set_grade(-95) runs, the sanity check identifies -95 as outside the 0 to 100 range.
  • The setter displays an error notification and refuses to update the private attribute.
  • Calling student1.get_grade() confirms that the private variable retained its previously valid value (95) rather than being corrupted by the invalid input.

Output:

Invalid grade: -95. Grade must be between 0 and 100.
Current Stored Grade: 95

6. Formatting Output Inside Getters

Getters are not limited to returning raw stored values. A getter can also transform internal data into user-friendly formats before returning it—for example, converting numerical marks into letter grades such as A, B, C, or D.

Lesson Q&A

In Python Object-Oriented Programming, getters are methods designed to retrieve or read private data attributes, while setters are methods designed to assign or update values to those attributes. Because encapsulation keeps private variables (prefixed with __) hidden from direct outside access, getters and setters act as a controlled public interface. They allow external programs to access and modify an object's internal data safely without exposing the raw variables directly.

A setter method acts as a gatekeeper by applying validation checks (sanity checks) to incoming values before updating an attribute. For example, in a Student class, a student's grade should fall between 0 and 100. If an external user attempts to set an invalid value (such as -20 or -95), the setter intercepts the input, rejects the update, and displays an error message. This preserves the integrity of the object's data and protects the class from being corrupted by improper inputs.

When a setter rejects an invalid value, the private attribute is not updated. Instead, the private variable safely retains its previously established valid value (such as the initial value assigned during constructor initialization). Calling the getter method afterward confirms that the internal state remained unchanged and protected.

What children can learn

WhatsApp +92 322 421 7059

AI and Robotics Club | The 100 Mission AI & Robotics Hero Journey

Recommended self-paced course for parents & educators

mBlock Coding for Kids on Udemy.

Disclosure: This page contains affiliate links. We may earn from qualifying purchases at no extra cost to you. Links are intended for parents and educators, not as marketing directed at young children.

Visual AI Extensions: Face Tracking and Models Without Syntax Fear

Puchong families gain from visual AI that stays supervised and age-appropriate. Students never share personal data with AI tools. Physical robots remain optional. Enquire +92 322 4217059.

Frequently asked questions

Why is mBlock recommended for teaching artificial intelligence to kids in Puchong?

mBlock integrates Scratch visual block coding with AI extension modules. This allows students in Puchong to create face-tracking programs, voice-activated controls, and machine learning models easily without getting stuck on complex code syntax.

Can Puchong students connect mBlock software with physical robotics hardware?

Yes! mBlock connects directly to mBot hardware, Arduino boards, and sensors, enabling students to write visual AI programs that control real-world robotic motors and lights.

How can I request a free mBlock demo class for my child in Puchong?

Click the WhatsApp button on this page to connect with our mentor team and arrange a free live trial session this week.

Explore related learning

Subang JayaAI STEM · Subang Jaya PJScratch · Petaling Jaya AmpangRobotics Club · Ampang