DEV Community

velvizhi Muthu
velvizhi Muthu

Posted on

Module 3: Encapsulation

01.What is Encapsulation?

Encapsulation means wrapping (hiding) variables (data) and providing access only through methods (getters/setters).

Real-Time Example: Bank Account :

Account balance should not be directly accessible.

Only deposit/withdraw methods should modify it.

Getters provide safe access to balance.
Enter fullscreen mode Exit fullscreen mode

02.Access modifier types?
1.public
2.protected
3.default
4.private

03.what is public?

public → accessible everywhere.
public → Surname (everyone knows).

ex:
public → everyone can know
public int dad_amount = 1000;

04.What is the protected?

protected → accessible within the same package and by subclasses (even in other packages).

protected → Property (children can inherit, brother in the same family can access).

ex:
protected → accessible to all children (even in other packages)
protected String property = "House";

05.What is the default?

default (no modifier) → accessible only within the same package.
default → Family name (known inside same package/family).
ex:
default → only family members in the same package can access
String familyName = "Kumar Family";

06.What is the private?
private → accessible only within the same class
private → Secret (only parent knows).

ex:
private → only parent knows
private String secret = "Parent's Secret";

07.What is the Import statement?
Import is a keyword.
We can use it to access the other package. That time we call the "import" keyword.
Import keyword, no need to call the same package.

08.What is return?
return is a keyword in java
return mainwork is retunted the variable value.
if i use the return keyword,then no need to call the object.

09.What is the purpose of poja class?
POJO classes are used to create readable and reusable code,
variable used to create the get method and get the value.

10.What is get balance?
Retrieves current account balance.

11.What is set balance?
Updates value of private balance.

Top comments (0)