Variables declaired as private in one class can be accessed into another class without using inheritance i.e extend and obj...? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Variables declaired as private in one class can be accessed into another class without using inheritance i.e extend and obj...?

Encapsulation concept. Can a Data hidden in one class be accessed into another class..?

30th Jun 2017, 5:32 PM
Saikrishna Thatikonda
Saikrishna Thatikonda - avatar
1 Answer
+ 3
private variables cannot be accessed from outside the class directly, that's what private does. To access the private value indirectly from outside the class use a getter/accessor. (A public method that returns the private value for you) Example/ class Person{ private int age; public int getAge(){ return this.age; } } Now I can call getAge() whenever to get the private variables value.
30th Jun 2017, 5:38 PM
Rrestoring faith
Rrestoring faith - avatar