1

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()?

Bob
  • 436
  • 5
  • 13
  • possible duplicate of [PHP: Public, Private, Protected](http://stackoverflow.com/questions/4361553/php-public-private-protected) – DanMan Jul 19 '14 at 12:11
  • The precise semantics of these keywords are language-specific. Not all OOP languages have these concepts, and not all implementations enforce every requirement of the specification. It would probably make sense to change this into a C++ question (or whichever language you actually need help with). – tripleee Jul 19 '14 at 12:11
  • It's true, C++ and Java have these concepts, I'll add the tags. – Bob Jul 19 '14 at 12:12
  • @Bob Warning : Java has "package-private" (no specifier), C++ does not. – Quentin Jul 19 '14 at 12:15
  • Fields can't "access into" anything... – user253751 Jul 19 '14 at 12:15
  • @Quentin thanks, tag deleted. – Bob Jul 19 '14 at 12:17
  • 1
    possible duplicate of [In Java, what's the difference between public, default, protected, and private?](http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private) – Utsav T Jul 19 '14 at 12:19

3 Answers3

1

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.

Rahul Vishwakarma
  • 996
  • 5
  • 17
  • 35
0
  • private - Only the class protected

  • Protected Those that inherit

  • Public the world
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

What you're talking about are called access-modifiers in java. You have mentioned three of them but there are in total 4 types of access modifiers :

  1. private - They are not visible to anybody except to members of the class they reside in.
  2. protected - private + Visible to sub-classes as well.
  3. default - private + other classes in the same package.
  4. public - Visible to the entire universe.
abbasdgr8
  • 555
  • 1
  • 5
  • 15