+ 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..?
1 Antwort
+ 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.