Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Get and Set | Encapsulation
Object-Oriented Programming in JavaScript
course content

Course Content

Object-Oriented Programming in JavaScript

Object-Oriented Programming in JavaScript

1. Classes & Objects
2. Encapsulation
3. Static
4. Inheritance

Get and Set

For direct access to the class properties, you can use two access methods - get and set.

get lets you read only some private data, but not change it.

set provides an opportunity to change the value of some attributes.

This approach also demonstrates the encapsulation principle: some attributes you can only read, but not modify, and some attributes you can modify only.

In other words, you can not access the private attribute name, but after adding get(), you'll have an opportunity to extract the value of student.name, but not to change it. But if you add set(value) to this attribute, you may change the student.name.

You can ask: why name attribute is private if we can modify it and read it using get and set? The reason is that inside set(value) method, you can validate the value before assigning the attribute. Look at the example with age:

First time, age has wrong value, so by default student.age will be 18. In the second case, when we assign age with 64, everything is valid, so student.age is 64 now.

This way, you won't set some wrong values in the age attribute.

You can use either get or set method according to your aim.

Task

Add get and set for the gpa attribute. Let's say that GPA's possible value is between 1.0 and 4.0. In case of incorrect value, set the GPA to 1.0.

Task

Add get and set for the gpa attribute. Let's say that GPA's possible value is between 1.0 and 4.0. In case of incorrect value, set the GPA to 1.0.

Everything was clear?

Section 2. Chapter 2
toggle bottom row

Get and Set

For direct access to the class properties, you can use two access methods - get and set.

get lets you read only some private data, but not change it.

set provides an opportunity to change the value of some attributes.

This approach also demonstrates the encapsulation principle: some attributes you can only read, but not modify, and some attributes you can modify only.

In other words, you can not access the private attribute name, but after adding get(), you'll have an opportunity to extract the value of student.name, but not to change it. But if you add set(value) to this attribute, you may change the student.name.

You can ask: why name attribute is private if we can modify it and read it using get and set? The reason is that inside set(value) method, you can validate the value before assigning the attribute. Look at the example with age:

First time, age has wrong value, so by default student.age will be 18. In the second case, when we assign age with 64, everything is valid, so student.age is 64 now.

This way, you won't set some wrong values in the age attribute.

You can use either get or set method according to your aim.

Task

Add get and set for the gpa attribute. Let's say that GPA's possible value is between 1.0 and 4.0. In case of incorrect value, set the GPA to 1.0.

Task

Add get and set for the gpa attribute. Let's say that GPA's possible value is between 1.0 and 4.0. In case of incorrect value, set the GPA to 1.0.

Everything was clear?

Section 2. Chapter 2
toggle bottom row

Get and Set

For direct access to the class properties, you can use two access methods - get and set.

get lets you read only some private data, but not change it.

set provides an opportunity to change the value of some attributes.

This approach also demonstrates the encapsulation principle: some attributes you can only read, but not modify, and some attributes you can modify only.

In other words, you can not access the private attribute name, but after adding get(), you'll have an opportunity to extract the value of student.name, but not to change it. But if you add set(value) to this attribute, you may change the student.name.

You can ask: why name attribute is private if we can modify it and read it using get and set? The reason is that inside set(value) method, you can validate the value before assigning the attribute. Look at the example with age:

First time, age has wrong value, so by default student.age will be 18. In the second case, when we assign age with 64, everything is valid, so student.age is 64 now.

This way, you won't set some wrong values in the age attribute.

You can use either get or set method according to your aim.

Task

Add get and set for the gpa attribute. Let's say that GPA's possible value is between 1.0 and 4.0. In case of incorrect value, set the GPA to 1.0.

Task

Add get and set for the gpa attribute. Let's say that GPA's possible value is between 1.0 and 4.0. In case of incorrect value, set the GPA to 1.0.

Everything was clear?

For direct access to the class properties, you can use two access methods - get and set.

get lets you read only some private data, but not change it.

set provides an opportunity to change the value of some attributes.

This approach also demonstrates the encapsulation principle: some attributes you can only read, but not modify, and some attributes you can modify only.

In other words, you can not access the private attribute name, but after adding get(), you'll have an opportunity to extract the value of student.name, but not to change it. But if you add set(value) to this attribute, you may change the student.name.

You can ask: why name attribute is private if we can modify it and read it using get and set? The reason is that inside set(value) method, you can validate the value before assigning the attribute. Look at the example with age:

First time, age has wrong value, so by default student.age will be 18. In the second case, when we assign age with 64, everything is valid, so student.age is 64 now.

This way, you won't set some wrong values in the age attribute.

You can use either get or set method according to your aim.

Task

Add get and set for the gpa attribute. Let's say that GPA's possible value is between 1.0 and 4.0. In case of incorrect value, set the GPA to 1.0.

Section 2. Chapter 2
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt