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.