Lesson 27 of 34

Encapsulation: private, Getters, Setters

9 min 35 XP

Fields private ആക്കൂ, അപ്പോൾ class-ന് പുറത്തുള്ള കോഡിന് അവ നേരിട്ട് മാറ്റാനാവില്ല. Getter value വായിക്കുന്നു; setter പരിശോധിച്ച ശേഷം മാറ്റുന്നു. ഇതാണ് encapsulation, OOP-യുടെ നാല് തൂണുകളിൽ ഒന്ന്.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

OOP-യുടെ നാല് തൂണുകൾ: Encapsulation, Inheritance, Polymorphism, Abstraction. Interviews-ൽ ഓരോന്നിനും ഉദാഹരണം നൽകാൻ തയ്യാറായിരിക്കൂ.

Challenge

+35 XP

private int age (18-ൽ തുടങ്ങുന്നു), 1 മുതൽ 100 വരെ മാത്രം സ്വീകരിക്കുന്ന setAge(int age) (അല്ലെങ്കിൽ "Invalid age" print ചെയ്ത് പഴയ value നിലനിർത്തുന്നു), getAge() എന്നിവയുള്ള class Student എഴുതൂ. main()-ൽ ഒരു സംഖ്യ വായിച്ച് setAge call ചെയ്യൂ, പിന്നെ "Age: " + getAge() print ചെയ്യൂ. Input: 150 Output: Invalid age Age: 18

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.