KwickAcademy Java · 5 min · free
Access Specifiers, Encapsulation and Visibility
The four access levels, how private protects data, encapsulation with getters and setters, and the scope and lifetime of every kind of variable.
Follows the syllabus of: CBSE Class 9 Information Technology (402), ICSE Class 9 Computer Applications, ICSE Class 10 Computer Applications, ISC Class 11 Computer Science (868)
On screen in this lesson
What is an access specifier?
| A keyword that decides who can use a member |
| Members: variables, methods and constructors |
| Package: a folder that groups related classes |
| Four levels: private, default, protected, public |
The four access levels
| Specifier | Visible in | Typical use |
|---|---|---|
| private | own class only | data fields |
| default (none) | same package | package helpers |
| protected | package + subclass | parent data |
| public | everywhere | main, getters |
Encapsulation
| Wrap data and methods together in one class |
| Hide the data by making it private |
| A getter method reads the value |
| A setter method changes it, after a check |
Scope and lifetime
| Scope: the part of code where a name can be used |
| Lifetime: how long the variable exists in memory |
| A block is code between { and } |
| A variable declared in a block dies when it ends |
Four kinds of variables
| Variable | Scope | Lifetime |
|---|---|---|
| Local | its block | until block ends |
| Argument | its method | until method ends |
| Instance | whole class | while object lives |
| Class (static) | whole class | while program runs |
Visibility rules for exams
| private: not visible outside, not even in subclasses |
| protected: visible in subclasses and the package |
| public: visible from any class |
| Local variables never take an access specifier |
| A top level class is public or default only |
Quick answers
Can a subclass use its parent's private data?
No. private stays hidden even from a subclass; use protected for that.
Why did the balance stay 100 after setBal(-50)?
The setter checked the value and refused a negative amount.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
What do you get if you write no access keyword?41 sec
How do you stop a negative balance?38 sec
Why does my loop variable vanish?41 sec
Can a subclass use a private member?44 secThe full lesson, in text
Hello students, welcome to Kwickprep. Should any stranger be able to set your bank balance to zero? Of course not. Today we will learn how Java protects data with access specifiers and encapsulation, and how long each variable lives.
First, some new terms. An access specifier is a keyword that decides who can use a member of a class. Members are the variables, methods and constructors inside a class. A package is like a folder that groups related classes together. Java has four access levels, private, default, protected and public.
Here are the four levels, from the most closed to the most open. Private means the member can be used only inside its own class. Default is what you get when you write no keyword, and it allows the same package only. Protected allows the same package, plus subclasses, which are child classes, even in other packages. Public means the member can be used from everywhere.
Here bal, the balance, is private inside the class Bank. The class Thief tries to set it to zero. This does not compile. The error says, bal has private access in Bank. So private data is safe from other classes.
This protection leads to an important idea called encapsulation. Encapsulation means wrapping the data and the methods that use it together in one class. We hide the data by making it private, and this is called data hiding. A getter is a method that returns the value, so others can read it. A setter is a method that changes the value, but only after checking it is valid.
Here the balance is private, and starts at one hundred. Get bal returns it, and set bal changes it only if the new amount is zero or more. In main, the variable k tries to set the balance to minus fifty. Pause and predict, what prints? It prints one hundred, because the setter refused the negative value.
Now, two words that exams love. Scope is the part of the program where a variable's name can be used. Lifetime is how long the variable stays in memory. A block is any code between an opening and a closing curly bracket. A variable declared inside a block can be used only there, and it disappears when the block ends.
Java has four kinds of variables. A local variable is declared inside a method or block, and lives until that block ends. An argument variable is a parameter, and lives until the method returns. An instance variable is used in the non-static code of the class, and lives as long as its object. A class variable is static, and lives while the program runs.
Here total is a local variable of main. The loop counter, the variable i, belongs only to the for loop. The loop adds one, two and three, so total becomes six. After the loop ends, the counter no longer exists. If we tried to print it after the loop, Java would say, cannot find symbol.
Board exam questions often test these visibility rules, so learn them well. A private member is never visible outside its class, not even to a subclass. A protected member is visible to subclasses and to classes in the same package. A public member is visible from any class. Local variables never take an access specifier, so writing private inside a method is an error. And a top level class can only be public or default.
Let us revise what we learned today. The four access levels, from closed to open, are private, default, protected and public. Encapsulation keeps data private, with public getters and setters. Scope is where a name can be used, and lifetime is how long it exists. Remember the four kinds, local, argument, instance and class variables. And a private member stays hidden even from subclasses.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 9 Information Technology (402) | Introduction to IT-ITeS Industry |
| ICSE Class 9 Computer Applications | Introduction to Object Oriented Programming Concepts |
| ICSE Class 10 Computer Applications | Encapsulation |
| ISC Class 11 Computer Science (868) | Statements, Control Flow and Functions |
| ISC Class 12 Computer Science (868) | Inheritance, Interfaces and Polymorphism |
| GSEB Std 12 Computer Studies | Classes, Objects, Arrays and Strings in Java |
| Programming All levels Java | Classes, Objects and Methods |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

