Search code examples
javaooppseudocode

OOP - Who can access to public, private, protected variable?


I'm new to OOP. Look at following pseudo code:

Class Test{
   public String a;
   protected String b;
   private String c;
   public void aa(){}
   protected void bb(){}
   private void cc(){}
   Class Test2{
      private void dd(){}
   }
}
Class Test3 extends Test{
   private void ee(){}
}
Class Test4{
   private void ff(){}
}

Can a, b and c access into aa(), bb() and cc()? Can a, b and c access into the class Test2 and dd()? Is true that only a and b can access into the class Test3 and ee()? Is true that only a can access into the class Test4 and ff()?


Solution

  • For the first question
    "Can aa() access a,b,c of class Test" : Yes it can access member of its outer class. Test2 is an inner class and an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields

    for second question
    "Is true that only a and b can access into the class Test3" : Yes a,b can be accessible inside class Test3. Subclass can access Public and Protected members of its base class.

    for third one
    "Is true that only a can access into the class Test4?" : Yes, only 'a' can be accessed inside class Test4 if you make an object of class Test and access it by using dot(.) operator.